How to reload current route in ember.js?

by raphael_tillman , in category: Javascript , 2 months ago

How to reload current route in ember.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cali_green , 2 months ago

@raphael_tillman 

You can reload the current route in Ember.js by using the refresh method.


Here's an example of how you can reload the current route in a controller or component:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import { inject as service } from '@ember/service';
import Controller from '@ember/controller';

export default Controller.extend({
  router: service(),

  actions: {
    refreshRoute() {
      this.get('router').transitionTo({
        queryParams: this.get('currentRoute.queryParams'),
        routeName: this.get('currentRoute.routeName')
      });

      // Or you can also use the following syntax
      // this.get('router').transitionTo(this.get('currentRoute'));
    }
  }
});


In this example, the refreshRoute action uses the router.transitionTo method to reload the current route by transitioning to the same route with the same query parameters.


You can then trigger this action in your template or in your code when you want to reload the current route.