How to fire an event to Ember from another framework

前端 未结 2 1477
误落风尘
误落风尘 2020-12-02 08:16

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 v

相关标签:
2条回答
  • 2020-12-02 08:33

    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.

    0 讨论(0)
  • 2020-12-02 08:33

    You probably shouldn't be doing this but here's a way to get access to the application's controllers, views, models and router instances. When your application is initialized, controllers, views, models and router are all registered in the application container __container__

    APP.__container__.lookup('controller:foo').get('content');
    APP.__container__.lookup('view:foo').get('templateName');
    APP.__container__.lookup('router:main');
    

    What i think you should do is encapsulate calls to the 3rd party library inside Ember and let Ember manage the whole application. See this attempt to make JQuery UI ember-aware

    0 讨论(0)
提交回复
热议问题