如何从另一个框架向Ember发起一个事件

Tom*_*Key 17 ember.js

我们正在使用版本pre4 of ember.

我们有一个与ember并行工作的框架(SignalR),可以处理我们应用程序的实时通知.在旧版本的ember中,我们能够访问路由器/控制器的全局引用.但随着Ember的新版本,这已不再可能.(这很好)我们尝试过不同的方法,比如在顶部路径中设置全局控制器:

setupController: function(){
    app.appController = this.controllerFor('app');
}
Run Code Online (Sandbox Code Playgroud)

并向该控制器发送一个事件,该事件冒泡到这样的路径:

notificator.update = function (context) { 
    app.appController.send('notificationOccured', context);
});
Run Code Online (Sandbox Code Playgroud)

但这感觉就像对抗Ember团队一样,刚刚删除了全球参考资料.

所以现在提出一个大问题:是否有更好的方法从Ember外部访问路由器或控制器?优选地,将事件发送到具有上下文的事件.

所有帮助表示赞赏!

Mik*_*tti 36

所以现在提出一个大问题:是否有更好的方法从Ember外部访问路由器或控制器?优选地,将事件发送到具有上下文的事件.

是.这听起来非常适合于余烬仪器模块.让适当的控制器订阅SignalR事件,然后在应用处理实时通知时触发它们.

首先,向ApplicationController添加一个方法以处理更新.如果未在此处定义,则事件将冒泡到路由器.

App.ApplicationController = Ember.Controller.extend({
  count: 0,
  name: 'default',
  signalrNotificationOccured: function(context) {
    this.incrementProperty('count');
    this.set('name', context.name);
  }
});
Run Code Online (Sandbox Code Playgroud)

接下来,通过订阅signalr.notificationOccured事件来设置ApplicationController .使用before回调记录事件并将其有效负载发送到控制器.

App.ApplicationRoute = Ember.Route.extend({
  setupController: function (controller, model) {
    Ember.Instrumentation.subscribe("signalr.notificationOccured", {
      before: function(name, timestamp, payload) {
        console.log('Recieved ', name, ' at ' + timestamp + ' with payload: ', payload);
        controller.send('signalrNotificationOccured', payload);
      },
      after: function() {}
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

然后从SignalR应用程序,用于Ember.Instrumentation.instrument将有效负载发送到ApplicationController,如下所示:

notificator.update = function (context) { 
  Ember.Instrumentation.instrument("signalr.notificationOccured", context);
});
Run Code Online (Sandbox Code Playgroud)

我在这里发布了一份带有模拟SignalR通知的工作副本:http://jsbin.com/iyexuf/1/edit

可以在此处找到仪器模块上的文档,还可以查看规范以获取更多示例.