简体   繁体   English

Ember.js 2触发路由时触发控制器动作

[英]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. 所以init()仅在应用启动时被调用一次,但是每当'modeller'路线被点击时,我都需要调用它。

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. ...这几乎可以正常工作-已记录“路由器中的didTransition”,但随后我无法引用该控制器。 It says "this.get(...) is undefined"?? 它说“ this.get(...)是未定义的”?

Any help appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

Take advantage of didTransition hook in modeller route: 利用modeller路线中的didTransition钩子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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