Ember.js - 将事件传递到控制器/路由器链上方

6
在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');
    }
  }
});

在CarsController中,myEvent处理程序是否有一种方法可以设置其内部状态变量,同时将事件传递到应用程序路由?我知道这个:

App.__container__.lookup('router:main').send('myEvent', obj);

但它使用了Ember的内部机制。我希望有一种更标准的方法来做这件事。

1个回答

6
这个的实现方法之一可能是:
App.SampleController = Ember.ObjectController.extend({
  needs: 'application',
  myEvent: function(obj) {
    this.set('aVariable', true);

    this.get('controllers.application').send('myEvent');
  }
});

正在工作中 演示

希望它能有所帮助。


无效(尝试使用Ember.js 1.0.0-rc.6)。我想知道是否是这个问题,所以我尝试了返回true和false,但都没有起作用。 - kleinsch
@kleinsch 还添加了一个演示 jsbin:http://jsbin.com/oduhom/3/edit 将事件发送到应用程序控制器应该可以工作。 - intuitivepixel
@kleinsch取消了弃用,使用needs API现在是获取依赖于另一个控制器的正确方式,对于所有的麻烦表示抱歉。 - intuitivepixel
太棒了!controllerFor已经过时,所以你应该像这样做-http://jsbin.com/ozecoj/1/edit。更新答案中的代码,我会接受的! - kleinsch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接