我正在尝试使用Ember Data的hasMany字段来返回一个数组,并将数组中的项计数作为计算属性.但是,当我执行以下操作时,它会返回一个对象(由于{async:true}似乎是一个promise)?而不是我期望的数组.
App.Shift = DS.Model.extend({
name: DS.attr('string'),
people: DS.hasMany('person', {async: true});
number_of_people: (function(){
this.get('people').length
}).property('people')
)};
App.Person = DS.Model.extend({
first_name: DS.attr('string'),
last_name: DS.attr('string')
});
Run Code Online (Sandbox Code Playgroud)
更新:我想回报人的长度.我试过这个但是当我访问该属性时,我得到了仍然返回的promise对象,而不是当时返回的完成promise的值.如何获得承诺返回的评估值?
number_of_people: (function(){
return this.get('people').then(function(people){
return people.get('length');
});
})
Run Code Online (Sandbox Code Playgroud) 我想知道Ember.Objects的创建方式是否有变化.考虑下面的代码,我不希望FULL_NAME,因为我没有使用"获得()"属性访问返回"张三",但最近发现这个工作并没有返回'张三’.什么时候使用简单的点符号是安全的,什么时候应该使用".get('property')"?
注意 - 这段代码适用于ember 1.7
var Person = Ember.Object.extend({
first_name: '',
last_name: '',
full_name: function() {
return this.first_name + ' ' + this.last_name;
}.property()
});
var joe = Person.create({first_name: 'Joe', last_name: 'Smith'});
console.log(joe.get('full_name'));
Run Code Online (Sandbox Code Playgroud)