Cokorda Raka
Cokorda Raka

Reputation: 4535

Ember calling refresh on route does not cause the renderTemplate to be invoked

I wanted to make the page to refresh automatically once a property of controller's model is updated.

I'm following this tip: How to reload current route in Ember.js?

So, I have an action method "runSimulation" in my controller, at the end of it I have this line :

this.send("sessionChanged");

In the associated route, I have:

actions: {
  sessionChanged: function() {
    console.log('YEAH');
    var transition = this.refresh();
    console.log(transition);
  }
},

renderTemplate: function(controller, model) {
  console.log('DINGDONG');
  var model = this.currentModel;
  if (model.simulation.params == undefined) {
    this.render('gobernador/crear-simulacion');
  } else {
    this.render('gobernador/show-simulacion');
  }
}

I could see that YEAH gets printed (meaning: the "sessionChanged" event sent by controller was successfully caught by the handler in the route object)..., but I don't see DINGDONG gets printed.

I'm using ember-cli, and I have the log transition enabled. I could see this in my javascript console:

Transitioned into 'gobernadores.gobernador.simulacion'

(which is expected). I suppose transitioning to "gobernadores.gobernador.simulacion" will cause the renderTemplate to be called (which for some reason is not happening here).

What might give us a clue here maybe the value of "transition" object returned from the execution of "refresh". In my case it gives:

{state: TransitionState, intent: C, **isActive: false,** router: Router, data: Object, resolvedModels: Object…} _visibleQueryParams: Objectdata: Object, handlerInfos: Array[4], intent: C, params: Object, pivotHandler: Class, promise: PromisequeryParams: Object, resolveIndex: 4,resolvedModels: Objectrouter: Routersequence: 4, state: TransitionStatetar, getName: "gobernador.simulacion"}

This "isActive" is false. Could it be the cause? If yes, why "isActive" is false?

I checked the API doc of Ember.Route::refresh ( http://emberjs.com/api/classes/Ember.Route.html#method_refresh ) ...

Refresh the model on this route and any child routes, firing the beforeModel, model, and afterModel hooks in a similar fashion to how routes are entered when transitioning in from other route. The current route params (e.g. article_id) will be passed in to the respective model hooks, and if a different model is returned, setupController and associated route hooks will re-fire as well.

So... maybe my question boil down to...: what conditions should be fulfilled for the refresh method of route to return a transition whose isActive is true?

I'm using ember 1.10.0

Thanks, Raka

UPDATE

I'm putting this link here..., just in case it gives some help in analysing the situation: http://blog.trackets.com/2013/02/08/router-request-lifecycle.html

Upvotes: 8

Views: 4080

Answers (2)

Bogdan Zurac
Bogdan Zurac

Reputation: 6451

What I ended up doing was, like torazaburo initially said, calling renderTemplate() manually. You can do so by returning true inside your sessionChanged action in the controller, if you intend to implement it there instead. This will bubble the action to your route. Then inside your route, just implement the same sessionChanged action (like you already did) and call this.renderTemplate().

What I also noticed is that when the user is not on the specified route, the action won't bubble anymore to the route from the controller (even if you transition to it), thus your route action won't be called anymore. In this case, apparently setupController() is called instead. Thought it was worth mentioning.

Upvotes: -1

user663031
user663031

Reputation:

I don't know why refresh doesn't re-render, but why don't you just call renderTemplate yourself?

But more basically, this is an anti-pattern. You are essentially trying to manage your own subroutes each with its own template, by remembering which one you want and calling render yourself on the right one. But that's what the Ember router does for a living. Just create multiple subroutes--one for crear-simulation and one for show-simulation, and let Ember do the work.

Assuming you want to do things this way, swapping templates in and out, your approach is still not very Ember-like. You should use the templateName property on the route. Put an observer on it to call the default renderTemplate when it changes. Get rid of your custom renderTemplate

Upvotes: 6

Related Questions