steve richey
steve richey

Reputation: 505

Get controller for model in Ember

If I have a reference to a model in Ember, how do I access its controller? If you're in the controller and want to access the model it's as simple as .get("model") but the reverse does not seem to hold when you need the controller.

Upvotes: 0

Views: 86

Answers (1)

Stephen Wright
Stephen Wright

Reputation: 2956

You can calculate the combined totals in the parent model, like so:

total: function() {
    var amount = 0;
    this.get('years').forEach(function(item) {
        amount += item.get('total');
    });
    return amount;
}.property('[email protected]')

Where "years" is the child model from the hasMany relationship. This will be dynamically updated any time any of the children change value, using the @each keyword.

See this full jsfiddle

Upvotes: 1

Related Questions