Search code examples
eventsextjscustom-event

ExtJS add custom event to body


is to possible to attach custom events to the document's body? I wanna implement a simple message bus. I did it many times with jQuery, but not sure how to do it in ExtJS.

Thank you.


Solution

  • You don't need to attach the event to the body or any DOM element, I would rather attach them to a dedicated object.

    Like :

    MyApp.MessageBus = Ext.extend(Ext.util.Observable, {
    
      // I don't think it's necessary to declare all events
      events : {
        sayHello : true
      }
    
    
    });
    
    MsgBus = new MyApp.MessageBus();
    

    And, somewhere in your code :

    MsgBus.on('sayHello', function(who) { alert("Hello, " + who); });
    

    And, on another place :

    MsgBus.fireEvent('sayHello', 'innerJL');