jeudi 18 juin 2015

An issue with data and routes in Ember

I am new to Ember.js and I have an issue I can't understand.

When I directly go to index.html#/subjects/1, I have the following data loaded:

  • subject (1):
    • Id: 1 - Title: My subject
  • message (3):
    • Id: 1 - Html content: undefined - Creation date: undefined
    • Id: 2 - Html content: undefined - Creation date: undefined
    • Id: 3 - Html content: undefined - Creation date: undefined
  • user (2):
    • Id: 1 - Name: undefined - Email: undefined - ...
    • Id: 2 - Name: undefined - Email: undefined - ...
    • Id: 3 - Name: undefined - Email: undefined - ...

And I also have an error (Uncaught TypeError: Cannot read property 'get' of null) on the following line :

return (this.get('sender').get('id') == 1);

But when I go on a page that loads all my fixture data and then go to index.html#/subjects/1, everything works fine.

To help you, this is my app.js file of Ember:

App = Ember.Application.create();

App.ApplicationAdapter = DS.FixtureAdapter;

App.Router.map(function() {
    // ...
    this.resource('subjects', function () {
        this.resource('subject', {path: ':subject_id'});
    });
});

// Some routes ...

App.SubjectRoute = Ember.Route.extend({
    model: function (params) {
        return this.store.find('subject', params.subject_id);
    }
});

// Some controllers ...

App.Subject = DS.Model.extend({
    title: DS.attr('string'),
    messages: DS.hasMany('message', { async: true }),
    users: DS.hasMany('user', { async: true }),

    messagesCount: function () {
        return this.get('messages.length');
    }.property('messages'),

    unreadMessagesCount: function () {
        return Math.floor(this.get('messages.length')/2);
    }.property('messages'),

    callsCount: function () {
        return Math.floor((Math.random() * this.get('messages.length')));
    }.property('messages'),

    textListOfUsers: function () {
        var res = '';
        var users = this.get('users').toArray();
        test = this.get('users');
        for (i = 0; i < users.length; i++) {
            var name = users[i].get('name');
            res = res.concat(users[i].get('name'), ' & ');
        };
        res = res.substring(0, res.length-3);
        return res;
    }.property('users.@each.name'),

    textListOfOtherUsers: function () {
        var res = '';
        var users = this.get('users').toArray();
        test = this.get('users');
        for (i = 0; i < users.length; i++) {
            var name = users[i].get('name');
            if (users[i].get('id') != 1)
                res = res.concat(users[i].get('name'), ' & ');
        };

        res = res.substring(0, res.length-3);
        return res;
    }.property('users.@each.name')
});

App.Message = DS.Model.extend({
    htmlContent: DS.attr('string'),
    creationDate: DS.attr('string'),
    subject: DS.belongsTo('subject'),
    sender: DS.belongsTo('user'),

    isFromConnectedUser: function () {
        return (this.get('sender').get('id') == 1);
    }.property('sender.id') // NOTE: Not sure about the syntax
});

App.User = DS.Model.extend({
    name        : DS.attr(),
    email       : DS.attr(),
    bio         : DS.attr(),
    avatarUrl   : DS.attr(),
    creationDate: DS.attr(),
    subjects: DS.hasMany('subject', { async: true }),
    messages: DS.hasMany('message', { async: true })
});

App.Subject.FIXTURES = [{
    id: 1,
    title: 'My subject',
    messages: [ 1,2,3 ],
    users: [1,2]
}, {
    id: 2,
    title: 'Hello world',
    messages: [ 4 ],
    users: [1,2]
// Other data...
}];

App.Message.FIXTURES = [{
    id: 1,
    htmlContent: 'Message 1',
    creationDate: '2015-06-16',
    subject: 1,
    sender: 1
}, {
    id: 2,
    htmlContent: 'Message 2',
    creationDate: '2015-06-16',
    subject: 1,
    sender: 2
// Other data...
}];

App.User.FIXTURES = [{
    id: 1,
    name: 'Alice Bernard',
    email: 'alice@bernard.com',
    bio: 'Lorem ispum dolor sit amet in voluptate fugiat nulla pariatur.',
    avatarUrl: 'images/default-user-image.png',
    creationDate: '2015-06-16'
}, {
    id: 2,
    name: 'John Doe',
    email: 'john@doe.com',
    bio: 'Lorem ispum dolor sit amet in voluptate fugiat nulla pariatur.',
    avatarUrl: 'images/default-user-image.png',
    creationDate: '2015-06-16'
// Other data...
}];

Thank you for your help.




Aucun commentaire:

Enregistrer un commentaire