Ember中的数据和路线问题

时间:2015-06-18 08:46:14

标签: ember.js

我是Ember.js的新手,我有一个我无法理解的问题。

当我直接转到 index.html#/ subjects / 1 时,我已加载以下数据:

  • 主题(1)
    • Id: 1 - 标题:我的主题
  • 消息(3)
    • Id: 1 - Html内容:未定义 - 创建日期: undefined
    • Id: 2 - Html内容:未定义 - 创建日期: undefined
    • Id: 3 - Html内容:未定义 - 创建日期: undefined
  • 用户(2)
    • Id: 1 - 名称:未定义 - 电子邮件: undefined - ...
    • Id: 2 - 姓名:未定义 - 电子邮件: undefined - ...
    • Id: 3 - 姓名: undefined - 电子邮件: undefined - ...

我在以下行中也有错误(Uncaught TypeError: Cannot read property 'get' of null):

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

但是当我进入一个加载我所有灯具数据然后转到 index.html#/ subjects / 1 的页面时,一切正常。

为了帮助你,这是我的Ember的app.js文件:

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...
}];

感谢您的帮助。

0 个答案:

没有答案