Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

ember.js - How to fire an event to Ember from another framework

We are using version pre4 of 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);
});

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

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.

All help is appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...