jeudi 19 février 2015

How to populate a Store with existing data coming from the main page in Ember

I use the DS.RESTAdapter to get / put / post / delete data by Ajax in an Ember application. This works fine. However, I would like to avoid the initial first load (GET) of data which gives a bit of a lagging impression to the User. I would like to populate the store with some bit of JSON embedded on the page (this is not a single page app). Notice, the post / put / delete should go as "normal" and be sent as request against the server.


Here is the initial code which works fine:



# Trigger a GET ajax request.
Tab2.ProfessionalEducationsRoute = Ember.Route.extend({
model: function() {
return this.get('store').find('professionalEducation');
}
});


I tried the "pushPayLoad" approach but no data seems loaded at all.



Tab2.ProfessionalEducationsRoute = Ember.Route.extend({

model: function() {
var postsJSON = {

"professionalEducations": [{

"id": 17,
"educationType": 1,
"year": 2014,
"title": "dpw",
"location": "yfw",
"comment": "",
"isPublic": true,
"rtfExport": true

}]
};

this.store.pushPayload('professionalEducation', postsJSON);

// Shall I return somethign here?
//return this.get('store').find('professionalEducation');
}
});


Following the Ember Guide, I also tried the "push" approach. Same result as above: no data loaded.



Tab2.ApplicationRoute = Ember.Route.extend({

model: function() {

this.store.push('professionalEducation', {

"id": 17,
"educationType": 1,
"year": 2014,
"title": "dpw",
"location": "yfw",
"comment": "",
"isPublic": true,
"rtfExport": true

});
console.log(321);
}
});


This post is related but I could not find a solution in there.


What would be wrong here?





Aucun commentaire:

Enregistrer un commentaire