samedi 18 avril 2015

EmberJS + EmberData: how to listen to model load from server

Environment:


Ember.VERSION = 1.7.1


DS.VERSION = 1.0.0-beta.14.1


Problem: I'm trying to run some code when the view is finally rendered on initial application load, the problem is the model is not yet loaded because the client fetches it from the server.


so the flow (I guess) that happens is something like this:



  1. Ember renders the view, requests the model

  2. Model returns a promise but view is rendered anyhow

  3. After AJAX is finished model is updated and view is re rendered


I want to run some code (bootstrap init of tooltips) after step 3 is completed, but I dont really sure how to.


just to point out, after the model is fetched from server when I leave and return to the same route everything is works fine (because the model returns from the store and not from server)


what I tried so far:


How can catch event after template is rendered at EmberJS?



App.ApartmentsView = Ember.View.extend({
didInsertElement: function () {
$('[data-toggle="tooltip"]').tooltip();
}
});


this didnt work, because at that point the model length is 0


listening to model change: App.ApartmentsRoute = Ember.Route.extend({ model: function () { return this.store.filter('apartment').then(function (model) { console.log(model); return model; }); } }); Again, at this point the model.content.length = 0 I guess at this point the store just creates the model and only at a later point updates it.


I've also tried listening to the arrayContentDidChange property in the controller, but at this point the model is not rendered yet, I can use a timeout but I think its a bad solution.


Any suggestions?


Aucun commentaire:

Enregistrer un commentaire