垃圾数据计算属性不适用于嵌套关联 [英] ember data computed property not working on nested association
本文介绍了垃圾数据计算属性不适用于嵌套关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的数据模型:
一个腿,有很多玩家,有很多回合:
App.Store = DS.Store.extend({
revision:11,
adapter:'DS.FixtureAdapter'
});
App.Leg = DS.Model.extend({
播放器:DS.hasMany('App.Player'),
turnCount:function(){
var i = 0;
this.get('players')forEach(function(p){
i + = p.get('turns.length');
});
return i;
} .property('players。@ each.turns.length')
});
App.Player = DS.Model.extend({
leg:DS.belongsTo('App.Leg'),
turn:DS.hasMany('App.Turn ')
});
App.Turn = DS.Model.extend({
播放器:DS.belongsTo('App.Player')
});
计算的属性 turnCount
不会自动当我创建一个新的Turn时,得到更新,但是它应该是对吗?
/ *
*控制台验证
* /
var leg = App.Leg.createRecord();
var player = leg.get('players')。createRecord();
leg.get('turnCount');
// => 0
player.get('turn')。createRecord();
leg.get('turnCount')
// => 0
更新
似乎如果你坚持一层嵌套的东西只是工作。所以:
.property('players。@ each.someProperty')
/ pre>
只要someProperty不是枚举。
解决方案尝试这样:
.property('players。@ each.turns。@ each')
您还可以使用
reduce
方法来计算总计:
<$ p $ ($($,$,$)code code code
返回this.get('players')。 .length');
},0);
} .property('players。@ each.turns。@ each')
I have a simple datamodel:
a Leg, has many Players, who have many Turns:
App.Store = DS.Store.extend({
revision: 11,
adapter: 'DS.FixtureAdapter'
});
App.Leg = DS.Model.extend({
players: DS.hasMany('App.Player'),
turnCount: function() {
var i = 0;
this.get('players').forEach(function(p) {
i+= p.get('turns.length');
});
return i;
}.property('players.@each.turns.length')
});
App.Player = DS.Model.extend({
leg: DS.belongsTo('App.Leg'),
turns: DS.hasMany('App.Turn')
});
App.Turn = DS.Model.extend({
player: DS.belongsTo('App.Player')
});
The computed property turnCount
doesn't automatically get updated when I create a new Turn, but it should, right?
/*
* do this on the console to verify
*/
var leg = App.Leg.createRecord();
var player = leg.get('players').createRecord();
leg.get('turnCount');
// => 0
player.get('turns').createRecord();
leg.get('turnCount')
// => 0
UPDATE
It seems that if you stick to one layer of nesting things just work. So:
.property('players.@each.someProperty')
works, as long as that someProperty is not an Enumerable.
解决方案
Try this:
.property('players.@each.turns.@each')
You could also use the reduce
method to calculate totals:
turnCount: function() {
return this.get('players').reduce(function(value, player) {
return value + player.get('turns.length');
}, 0);
}.property('players.@each.turns.@each')
这篇关于垃圾数据计算属性不适用于嵌套关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文