Reputation: 1882
I need to get the grand total price of all the objects returned from a JSON API.
my idea is to create a controller and set a property on it. Then grab the model loop over each object and add to the predefined property that objects total price. Something like this:
export default Ember.Controller.extend({
orderTotal: 0,
getOrderTotal: function(){
var model = this.get('model');
model.forEach(function(c){
var order_total = this.get('orderTotal') * c.total_price;
this.set('orderTotal', order_total)
});
}
});
problem is I can't get the model. How can I access the model inside a controller?
Upvotes: 1
Views: 60
Reputation: 663
The model is probably loaded asynchronously, therefore you will have to use .then(...) to wait for the model being loaded.
this.get('model').then(function(data) { ... create sum using `data` });
Though I would use computed properties to create the sum of something that can potentially change:
allPrices: Ember.computed.mapBy('model', 'price'),
totalPrice: Ember.computed.sum('allPrices')
http://emberjs.jsbin.com/wiwiqulozi/1/
Upvotes: 1