Trouble accessing controller's computed property from view

和自甴很熟 提交于 2019-12-13 01:14:28

问题


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') 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.

Any ideas?


回答1:


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'));
    }

});


来源:https://stackoverflow.com/questions/17726136/trouble-accessing-controllers-computed-property-from-view

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!