我是Ember.js的新手,我有一个我无法理解的问题。
当我直接转到 index.html#/ subjects / 1 时,我已加载以下数据:
我在以下行中也有错误(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...
}];
感谢您的帮助。