无法从视图访问控制器的计算属性 [英] Trouble accessing controller's computed property from view
问题描述
我的控制器具有计算所得的属性:
My controller has a computed property:
App.IndexController = Ember.ArrayController.extend({
grandTotal: function () {
return this.getEach('total').reduce(function(accum, item) {
return accum + item;
}, 0);
}.property('@each.total'),
});
但是我无法通过视图访问它.这是我的看法:
but I'm having trouble accessing it with my view. Here's my view:
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
,尽管我知道它已正确编码,因为它已在模板中呈现.出于其他原因,我需要在我的视图内访问它.
.get('length')
returns correctly, so I know when this is called the models are loaded. But grandTotal
is coming back as NaN
, even though I know it's coded correctly since it's being rendered in the template. I need to access it within my view for additional reasons.
有什么想法吗?
推荐答案
即使控制器的计算属性随@ each.total更改,该视图也仅关心控制器的属性.因此,该视图错误地观察了@each模型,而该模型本来应该观察 controller.grandTotal
:
Even though the controller's computed property changes with @each.total, the view only cares about the controller's property. Thus, the view was wrongly observing @each model, when it should have just been observing 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'));
}
});
这篇关于无法从视图访问控制器的计算属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!