简体   繁体   中英

Ember.js 2 trigger controller action when you hit it's route

I'm trying to get a graph redrawn every time I hit a certain controller/route. There are other pages where inputs are changed and the graph needs to redraw when the 'modeller' page is navigated to. Subsequent input changes on the modeller page cause a redraw but the graph is initially scaled incorrectly each time you navigate to it. So I need to do a redraw based on all the new inputs the moment you arrive on the 'modeller' page.

export default Ember.Controller.extend({
  calculator: Ember.inject.service(),

  needs: ['modeller'],

  init: function() {
    this.drawCharts();
  },
})

So the init() is only called once when the app starts, but I need something to call it whenever the 'modeller' route is hit.

I've tried adding this to the controller but to no avail:

doSomething: function() {
  console.log("currentPath");
  return;
}.observes('currentPath'),

And I've tried to trigger it from the router:

var Router = Ember.Router.extend({
  location: config.locationType,
  doSomething: function() {
    console.log("didTransition in router");
    this.get('controllers.modeller').init();
    return;
  }.on('didTransition'),
});

... which almost works - the "didTransition in router" is logged but I can't then reference the controller. It says "this.get(...) is undefined"??

Any help appreciated. Thanks.

Take advantage of didTransition hook in modeller route:

actions: {
  didTransition: function() {
    this.controller.drawCharts();
    return true; // Bubble the didTransition event
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM