Ember.js - 在控制器/路由器链上传播事件

kle*_*sch 6 javascript events router controller ember.js

在Ember.js中,未在控制器中处理的事件将路由器链向上传播到应用程序路由(有关详细信息,请参阅http://emberjs.com/guides/views/handling-events/).

有没有办法在控制器中定义一个事件处理程序,允许事件继续传播到路由器?

App.SampleController = Ember.ObjectController.extend({
  myEvent: function(obj) {
    this.set('aVariable', true);
  }
});

App.ApplicationRoute = Ember.Route.extend({
  events: {
    myEvent: function(obj) {
      this.transitionTo('something');
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

是否有一种方法可以让CarsController中的myEvent处理程序设置其内部状态变量,同时也可以将链上的事件踢到应用程序路径?我知道这件事:

App.__container__.lookup('router:main').send('myEvent', obj);
Run Code Online (Sandbox Code Playgroud)

但它使用了Ember内部.我希望有更标准的方法来做到这一点.

int*_*xel 6

实现这一目标的一种方法是:

App.SampleController = Ember.ObjectController.extend({
  needs: 'application',
  myEvent: function(obj) {
    this.set('aVariable', true);

    this.get('controllers.application').send('myEvent');
  }
});
Run Code Online (Sandbox Code Playgroud)

工作演示.

希望能帮助到你.