我的控制器有一个计算属性:
App.IndexController = Ember.ArrayController.extend({
grandTotal: function () {
return this.getEach('total').reduce(function(accum, item) {
return accum + item;
}, 0);
}.property('@each.total'),
});
但是我无法通过我的观点访问它。这是我的观点:
App.SummaryView = Ember.View.extend({
templateName: 'summary',
companiesChanged: function() {
Ember.run.once(this, 'logCompanies');
}.observes('controller.@each'),
logCompanies: function() {
console.log(this.get('controller').get('model').get('length'));
console.log(this.get('controller').get('grandTotal'));
}
});
.get('length')
正确返回,因此我知道何时调用此模型已加载。但grandTotal
会以NaN
的形式返回,即使我知道它已正确编码,因为它正在模板中呈现。我需要在我的视图中访问它,原因还有其他原因。
有什么想法吗?
答案 0 :(得分:1)
即使控制器的计算属性随@ each.total而变化,视图也只关心控制器的属性。因此,该视图错误地观察了@each模型,它应该只是观察controller.grandTotal
:
App.SummaryView = Ember.View.extend({
templateName: 'summary',
companiesChanged: function() {
Ember.run.once(this, 'logCompanies');
}.observes('controller.grandTotal'),
logCompanies: function() {
console.log(this.get('controller').get('model').get('length'));
console.log(this.get('controller').get('grandTotal'));
}
});