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

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

问题描述

我们正在使用版本pre4的ember。

We are using version pre4 of ember.

我们有一个框架(SignalR)与Ember并行工作,可以处理我们应用程序的实时通知。在旧版本的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 事件。使用以前的回调记录事件并将其发送给控制器。

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/edit

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天全站免登陆