Javascript EmberJS属性不会在创建记录时自动更新
我的应用程序有一个称为“余额”的模型,我们可以在其中添加金额和说明。我创建了一个操作,当你点击一个按钮时添加一些余额记录,到目前为止效果很好。问题是,我创建了一个名为“totalBalance”的控制器属性来从数据库检索和计算总量,它工作得很好,并显示在页面中,但在我添加新记录时它不会更新,我希望它实时更新 这是我的密码: 平衡控制器Javascript EmberJS属性不会在创建记录时自动更新,javascript,ember.js,ember-data,Javascript,Ember.js,Ember Data,我的应用程序有一个称为“余额”的模型,我们可以在其中添加金额和说明。我创建了一个操作,当你点击一个按钮时添加一些余额记录,到目前为止效果很好。问题是,我创建了一个名为“totalBalance”的控制器属性来从数据库检索和计算总量,它工作得很好,并显示在页面中,但在我添加新记录时它不会更新,我希望它实时更新 这是我的密码: 平衡控制器 App.BalanceController = Ember.ArrayController.extend({ totalBalance: functio
App.BalanceController = Ember.ArrayController.extend({
totalBalance: function() {
var total = 0;
this.store.all('balance').forEach(function(item) {
total = total + item.get('amount');
});
return total;
}.property('balance.@each'),
actions: {
generateBalance: function() {
var balance = this.store.createRecord('balance', {
amount: 1270.84,
description: 'Some dummy description for this balance.'
});
balance.save();
}
}
});
平衡模型
App.Balance = DS.Model.extend({
amount: DS.attr('number'),
description: DS.attr('string')
});
平衡路线
App.BalanceRoute = Ember.Route.extend({
model: function() {
return this.store.find('balance');
}
});
我认为问题在于您为totalBalance指定依赖键的方式。我会尝试这样做:
App.BalanceController = Ember.ArrayController.extend({
totalBalance: function() {
var total = 0;
this.forEach(function(item) {
total = total + item.get('amount');
});
return total;
}.property('content.@each.amount')
});
作为一个更简洁的选项,您还可以使用reduceComputed
属性指定执行的和