我有一个群组模型,其中包含来自多个用户的一些赌注。每个用户都可以有多个赌注。为了显示最终结果,我想按用户对投注进行分组,然后总结点数。这是我的赌注模型,其中包含点逻辑:
App.GameBet = DS.Model.extend({
scoreT1: DS.attr(),
scoreT2: DS.attr(),
betRound: DS.belongsTo('betround'),
game: DS.belongsTo('game', {async:true}),
user: DS.belongsTo('user', {async:true}),
points: function(){
var game = this.get('game');
var sc1 = this.get('game.scoreT1');
var sc2 = this.get('game.scoreT2');
var b1 = this.get('scoreT1');
var b2 = this.get('scoreT2');
if(sc1 == b1 && sc2 == b2) {
return 7;
} else if ((sc1 - sc2) == (b1 - b2)) {
return 5;
// Exakte Punktzahl
} else if ((sc1 - sc2) * (b1 - b2) > 0) {
return 3;
// Tendenz
} else {
return 0;
}
}.property('scoreT1', 'scoreT2', 'game.scoreT1', 'game.scoreT2')
});
当我用他们的积分渲染所有赌注时,这工作得非常好。但是当我使用自己的groupby ArrayComputeed函数时,它无法正常工作:
App.betGroupBy = function (dependentKey) {
var options = {
initialValue: Ember.A([]),
initialize: function (array, changeMeta, instanceMeta) {
return instanceMeta.previousKeys = {};
},
addedItem: function (accumulatedValue, item, changeMeta, instanceMeta) {
console.log(item.get('id') + " " + item.get('points'));
var user = item.get('user');
var key = user.get('id');
var group = accumulatedValue.findBy('key', key);
if (!group) {
group = Ember.Object.create({
key: key,
user: user,
bets: Ember.A([]),
betPoints: Ember.computed.mapBy('bets', 'points'),
points: Ember.computed.sum('betPoints')
});
accumulatedValue.pushObject(group);
}
group.get('bets').pushObject(item);
instanceMeta.previousKeys[Ember.guidFor(item)] = key;
return accumulatedValue;
},
removedItem: function (accumulatedValue, item, changeMeta, instanceMeta) {
var key = item.get(property);
var group = accumulatedValue.findBy('key', key);
if (!group) {
return;
}
var userBets = group.get('bets');
userBets.removeObject(item);
if (userBets.length === 0) {
accumulatedValue.removeObject(group);
}
return accumulatedValue;
}
};
return Ember.arrayComputed(dependentKey, options);
};
不知何故,我在分组函数中的“点”始终为零。 似乎承诺没有尽早得到解决或其他什么。