Sencha Touch 2 shares events between views and controllers

I need to share some events from my application so that they are shared between views and controllers. For example, the event: "updates from the server are available." To do this, I can run Firevent ("updatesAvailable") in the controller, but this forces other components to subscribe to the specific controller that fires this event.

What I want is to have a singleton object that will support all subscriptions to application logic.

Currently, I see that I can solve the problem by having one common instance of the view and subscribing to it all. But I would like to know if there is one of the Event Dispatcher boxes that knows about all the events inside the application.

+3
source share
1 answer

As far as I understand, you want to use the "Event Bus" template? You can use the global and only object that exists in every ST2 application - Ext.Viewport. At least I create in my application, and so far nothing is bad. Note. The best place to create an event handler is the controller's init () method.

Ext.define('Myapp.controller.ActivitiesController', {

extend : 'Ext.app.Controller',

requires : [],
    config: {
        refs: {
            myview: 'myview'
        }
...
    init: function () {
    var me = this;

    Ext.Viewport.on({
        scope: this,
        addactivitytype: function (config) {
                    var myview = me.getMyview(),
                        record = config.record
...

          });
     }
});

in another controller (or ever) you can write

 addActivityTypeTap: function (record) {
      ....
      Ext.Viewport.fireEvent('addactivitytype', {
        record: record
      });
 }
+3
source

All Articles