Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fire an event to Ember from another framework

Tags:

ember.js

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!

like image 250
TommyKey Avatar asked Jan 22 '13 12:01

TommyKey


1 Answers

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.

like image 146
Mike Grassotti Avatar answered Nov 02 '22 21:11

Mike Grassotti