Javascript 带有belongsTo的模型中的Computed属性返回未定义
我有三种型号Javascript 带有belongsTo的模型中的Computed属性返回未定义,javascript,ember.js,handlebars.js,Javascript,Ember.js,Handlebars.js,我有三种型号 // Product export default DS.Model.extend({ content: DS.attr('string'), creator: DS.belongsTo('user') }); // User export default DS.Model.extend({ email: DS.attr('string'), products: DS.hasMany('product'), person: DS.belon
// Product
export default DS.Model.extend({
content: DS.attr('string'),
creator: DS.belongsTo('user')
});
// User
export default DS.Model.extend({
email: DS.attr('string'),
products: DS.hasMany('product'),
person: DS.belongsTo('person'),
fullName: Ember.computed(function() {
return `${this.get('person.firstname')} ${this.get('person.surname')}`;
})
});
// Person
export default DS.Model.extend({
firstname: DS.attr('string'),
surname: DS.attr('string'),
users: DS.hasMany('user')
});
我试着把它用在车把上
{{#each products as |product|}}
{{product.creator.fullName}}
{{/each}}
如您所见,User
模型中有一个计算属性。但是它总是返回undefined-undefined
,因为this.get('person.firstname')
和this.get('person.姓氏')
返回undefined
余烬检查器显示每个模型的数据。知道如何解决这个问题吗?默认情况下,关系是异步的,并且返回。这意味着当你询问时,你没有数据。当您第一次询问数据时,它将被加载,并且您需要将从属密钥添加到您的数据库中,以便在解析
个人
模型时更新全名
另外,您可以查看像ember promise helpers
这样的插件来组织模板中的异步数据
// User
export default DS.Model.extend({
email: DS.attr('string'),
products: DS.hasMany('product'),
person: DS.belongsTo('person'),
fullName: Ember.computed('person.{firstname,surname}', function() {
return `${this.get('person.firstname')} ${this.get('person.surname')}`;
})
});
// Person
export default DS.Model.extend({
firstname: DS.attr('string'),
surname: DS.attr('string'),
users: DS.hasMany('user')
});
默认情况下,关系是异步和返回的。这意味着当你询问时,你没有数据。当您第一次询问数据时,它将被加载,并且您需要将从属密钥添加到您的数据库中,以便在解析
个人
模型时更新全名
另外,您可以查看像ember promise helpers
这样的插件来组织模板中的异步数据
// User
export default DS.Model.extend({
email: DS.attr('string'),
products: DS.hasMany('product'),
person: DS.belongsTo('person'),
fullName: Ember.computed('person.{firstname,surname}', function() {
return `${this.get('person.firstname')} ${this.get('person.surname')}`;
})
});
// Person
export default DS.Model.extend({
firstname: DS.attr('string'),
surname: DS.attr('string'),
users: DS.hasMany('user')
});
似乎您想要的是
Person
上的fullName
,而不是User
?这不是你的问题,但…事实上,这会导致相同的错误。似乎你希望个人
上有全名
,而不是用户
?不是你的问题,但…事实上,这会导致相同的错误。直到“依赖密钥”。。。ThanksTIL“依赖密钥”。。。谢谢