0
我的應用程序有一個名爲Balance的模型,我們可以添加數量和描述。當您點擊一個按鈕時,我創建了一個操作來添加一些餘額記錄,並且迄今爲止效果很好。問題是我創建了一個名爲「totalBalance」的控制器屬性來檢索和計算數據庫中的總量,它工作的很好,它顯示在頁面中,但當我添加一條新記錄時它不會更新,我希望它實時更新。EmberJS屬性在創建記錄時不會自動更新
這裏是我的代碼:
均衡控制器
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('[email protected]'),
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');
}
});
你太棒了!有用。 –