SuperMarco
SuperMarco

Reputation: 721

Emberjs - Knowing when model is rendered on template

Is it possible to know (with an event handler that would be the best) when the data from the model is render on the template, also when it's updated ?

I will use this as an example :

App.ApplicationRoute = Ember.Route.extend({
    setupController: function(controller, model) {
        var promise = myNewModel();
        this._super(controller, promise);
    }
})

I am updating the modal in my setupController because I don't want the user to be stuck on a blank page.

Is there is a way to get that event when all the data are done rendering on the template, in my case when I have updated the model ?

Upvotes: 0

Views: 45

Answers (2)

user663031
user663031

Reputation:

By the time you've reached setupController, the model needs to be a real one, not a promise. Setting the model here to be a promise is not going to work at all. You could try something like this:

setupController: function(controller, model) {
    myNewModel().then(function(model) {
        controller.set('model', model);
    });
}

In other words, it's not an event you're looking for, it's your newModel call fulfilling.

Upvotes: 1

selvagsz
selvagsz

Reputation: 3872

You should probably make use of Loading Substates. Refer docs here

You need to fetch the model from the route#model hook and return a promise. The loading state will be triggered until the promise resolves. You can show a loading template until the promise resolves.

App.ApplicationRoute = Ember.Route.extend({
 model: function() {
  var promise = myNewModel();
  return promise; 
 }
});

Refer @machty's jsbin and the related PR

Upvotes: 0

Related Questions