mardi 17 février 2015

Iterating through related models in Ember.js

I have the following models, which have basic associations, as you can see below:



App.PricingSummary = DS.Model.extend({
startDate: DS.attr(),
endDate: DS.attr(),
days: DS.hasMany('day', {async: true})
});

App.Day = DS.Model.extend({
date: DS.attr(),
rate: DS.belongsTo('rate', {async: true})
});

App.Rate = DS.Model.extend({
price: DS.attr()
});


The concept here is that a PricingSummary contains a set of Days, each of which has a Rate which is basically the price for that day. Rate needs to be its own model as there is supposed to be a finite set of values from which to assign to a day.


What I'm trying to do is in my controller, which has a context of a PricingSummary model, loop through the Days in the PricingSummary and find the lowest Rate for that period.


Here's my attempt at doing so:



barRange: function(){
var lowBar;

this.get('days').then(function(days){

days.forEach(function(day){

day.get('rate').then(function(rate){
rateValue = rate.get('price')

if(typeof lowBar === 'undefined' || rateValue < lowBar){
lowBar = rateValue;
}
});

});

console.log( 'lowBar is ' + lowBar )
});

}.property('days')


I think this is really only half-baked... but it shows you the way I'm approaching it, which may or may not be completely wrong. Is there a better way to do this? If not, what are the last pieces that I'm missing here?





Aucun commentaire:

Enregistrer un commentaire