如何从另一个框架向 Ember 触发事件 [英] How to fire an event to Ember from another framework

查看:19
本文介绍了如何从另一个框架向 Ember 触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用的是 ember 的 pre4 版本.

We are using version pre4 of ember.

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

We have a framework (SignalR) working parallel with ember that handles real-time notifications to our application. In the older versions of ember we were able to access the global reference of the router / controller. But with the new version of Ember this is no longer possible. (This is fine) We have tried different approaches like setting up a global controller in the top route:

setupController: function(){
    app.appController = this.controllerFor('app');
}

并向这个控制器发送一个事件,它会像这样冒泡到路由:

and sending an event to this controller, which bubbles up to the route like this:

notificator.update = function (context) { 
    app.appController.send('notificationOccured', context);
});

但这感觉就像是在对抗刚刚删除全局引用的 Ember 团队.

But this feels like working against the Ember team which just removed the global references.

那么现在是一个大问题:有没有更好的方法可以从 Ember 外部访问路由器或控制器?最好使用上下文向其中任何一个发送事件.

So now to the big question: is there a better way to access the router or a controller from outside Ember? Preferably send an event to either with a context.

感谢所有帮助!

推荐答案

那么现在是一个大问题:有没有更好的方法可以从 Ember 外部访问路由器或控制器?最好使用上下文向其中任何一个发送事件.

So now to the big question: is there a better way to access the router or a controller from outside Ember? Preferably send an event to either with a context.

是的.这听起来很适合 ember 仪表模块.让合适的控制器订阅 SignalR 事件,然后在您的应用处理实时通知时触发它们.

Yes. This sounds like a good fit for the ember instrumentation module. Have an appropriate controller subscribe to SignalR events, then trigger them whenever your app handles real-time notification.

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

First, add a method to ApplicationController for processing updates. If not defined here the event would bubble to the router.

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

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

Next, setup your ApplicationController by subscribing to the signalr.notificationOccured event. Use the before callback to log the event and send it's payload to the controller.

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() {}
    });
  }
});

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

Then from your SignalR Application, use Ember.Instrumentation.instrument to send payload to your ApplicationController as follows:

notificator.update = function (context) { 
  Ember.Instrumentation.instrument("signalr.notificationOccured", context);
});

我在此处发布了带有模拟 SignalR 通知的工作副本:http://jsbin.com/iyexuf/1/编辑

I posted a working copy with simulated SignalR notifications here: http://jsbin.com/iyexuf/1/edit

可以在此处找到有关检测模块的文档,还可以查看规范 了解更多示例.

Docs on the instrumentation module can be found here, also check out the specs for more examples.

这篇关于如何从另一个框架向 Ember 触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆