2014-01-19 59 views
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'); 
    } 
}); 

回答

2

我认为问题在于你指定totalBalance的依赖关键字的方式。我想尝试做这样的事情:

App.BalanceController = Ember.ArrayController.extend({ 

    totalBalance: function() { 
    var total = 0; 

    this.forEach(function(item) { 
     total = total + item.get('amount'); 
    }); 

    return total; 

    }.property('[email protected]') 
}); 

作为一个有点清洁选项,您还可以使用reduceComputed属性用于指定等进行here的总和。

+0

你太棒了!有用。 –