Skip to content Skip to sidebar Skip to footer

How To Reload Current Route In Ember.js?

in Ember.js I have route with model. Could you help me, when I'm on route playlist how to reload this route (or set new data to model) called by callback from another JS function?

Solution 1:

It seems the solution in the answer won't work for current route. I had a same issue and tried the solution here and it worked.

http://discuss.emberjs.com/t/refresh-current-view-page-after-language-change/4291/5#post_5

In your route.

actions: {
  sessionChanged: function() {
    this.refresh();
  }
}

and in your controller.

observeSession: function() {
  this.send("sessionChanged");
}.observes("session.isAuthenticated"),

Solution 2:

There are two ways of doing it.

One is write an action in playlist route and call this.refresh() inside it For more information you can visit Ember Guide refresh method for route.

The other way is in your controller depending on the situation when you need to reload your route use

this.get('target.target.router').refresh();

any of the two would help you in refreshing your route.

A small note of refresh method below from ember guides:

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.

Solution 3:

From a controller use transitionToRoute:

this.transitionToRoute('playlist', newModel);

From a route use transitionTo:

this.transitionTo('playlist', newModel);

For example, imagine you have an action on your controller

App.PlaylistController = Ember.ArrayController.extend({
 actions: {
   grabNewModel: function(){
     //get some new modelthis.transitionToRoute('playlist', newModel);
   }
 }
});

Solution 4:

This answer appears first on google when searching how to refresh a route with the current accepted answer being out of date. If you really need to refresh a route and perform the model hook action again from an action in a controller then use the following:

In the route

@action
refreshModel() {
  this.refresh();
}

In order to call this action in the controller use

this.send('refreshModel');

For example:

@action
performUpdate(event) {
  event.preventDefault();
  // Perform necessary actionthis.send('refreshModel');
}

Note: This will send the action to the corresponding route for the controller it is called from, and update that route only and any child routes.

Post a Comment for "How To Reload Current Route In Ember.js?"