lundi 23 mars 2015

Acceptance testing async models in ember.js

I have many async relationships in my ember-data driven application. I use a 'create default record' pattern in the router which I am having trouble getting to run properly in the application's test mode.


A report has a seriesSet. If the seriesSet is not already created I have to get some dynamic values from an endpoint, use the value returned, and fill in properties on the seriesSet.


The relevant parts of the model:



export default DS.Model.extend({
// snip
seriesSet: DS.belongsTo('series-set', { async: true }),
// snip
});


And the relevant parts of the router in question:



model: function(){
var self = this;
var report = this.modelFor('report');

return report.get('seriesSet').then(function(seriesSet){
if(seriesSet === null){
seriesSet = self.defaultSeriesSet();
}
return seriesSet;
});
},
defaultSeriesSet: function(){
var self = this,
backendService = this.get('computationBackendService');

return backendService.series().then(function(backendSeriess){
backendSeriess.setEach('selected', true);

var seriesSet = self.store.createRecord('seriesSet', {
seriess: backendSeriess
});

return seriesSet;
});
},


I'm not sure where to set the Ember.run statement. Shouldn't the promise resolver from model -> setupController take care of everything? And if not, how can Ember.run be used when I chain return statements (and not set a property from the backburner).





Aucun commentaire:

Enregistrer un commentaire