Ember.js Ember中的数据和路由问题
我是新加入Ember.js的,我有一个不明白的问题 当我直接转到index.html#/subjects/1时,我加载了以下数据:Ember.js Ember中的数据和路由问题,ember.js,Ember.js,我是新加入Ember.js的,我有一个不明白的问题 当我直接转到index.html#/subjects/1时,我加载了以下数据: 主题(1): Id:1-标题:我的主题 消息(3): Id:1-Html内容:未定义-创建日期:未定义 Id:2-Html内容:未定义-创建日期:未定义 Id:3-Html内容:未定义-创建日期:未定义 用户(2): Id:1-名称:未定义-电子邮件:未定义- Id:2-名称:未定义-电子邮件:未定义- Id:3-名称:未定义-电子邮件:未定义- 我
- 主题(1):
- Id:1-标题:我的主题
- 消息(3):
- Id:1-Html内容:未定义-创建日期:未定义
- Id:2-Html内容:未定义-创建日期:未定义
- Id:3-Html内容:未定义-创建日期:未定义
- 用户(2):
- Id:1-名称:未定义-电子邮件:未定义-
- Id:2-名称:未定义-电子邮件:未定义-
- Id:3-名称:未定义-电子邮件:未定义-
uncaughttypeerror:cannotreadproperty'get'of null
):
return (this.get('sender').get('id') == 1);
但是,当我进入一个页面,加载所有夹具数据,然后进入index.html#/subjects/1时,一切正常
为了帮助您,这是我的余烬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...
}];
App=Ember.Application.create();
App.ApplicationAdapter=DS.FixtureAdapter;
App.Router.map(函数(){
// ...
this.resource('subjects',function(){
this.resource('subject',{path:':subject_id'});
});
});
//有些路线。。。
App.SubjectRoute=Ember.Route.extend({
型号:功能(参数){
返回此.store.find('subject',params.subject\u id);
}
});
//一些控制器。。。
App.Subject=DS.Model.extend({
标题:DS.attr('string'),
消息:DS.hasMany('message',{async:true}),
用户:DS.hasMany('user',{async:true}),
MessageScont:函数(){
返回this.get('messages.length');
}.property(“消息”),
UnderMessageCount:函数(){
返回Math.floor(this.get('messages.length')/2);
}.property(“消息”),
callScont:函数(){
返回Math.floor((Math.random()*this.get('messages.length'));
}.property(“消息”),
textListOfUsers:函数(){
var-res='';
var users=this.get('users').toArray();
test=this.get('users');
对于(i=0;i
谢谢您的帮助。如果您从您的多个关系中删除
async:true
会怎么样?我没有成功地删除async:true
,没有出现错误。如果没有async:true
,我不知道如何转换夹具数据(我没有找到关于它的好文档)