Simple message bus for extjs 4 5


Here’s a simple message bus/broker I wrote for Ext.js 4

Ext.define('Ext.ux.MessageBroker', {
    extend: 'Ext.util.Observable',

    statics: {
        instance: null,
        setInstance: function(i) {
            this.instance = i;
        },
        sendMessage: function(msg, data) {
            this.fireEvent('message',{"msg":msg,"data":data});
        }
    },

    constructor: function(config){
        this.addEvents({
            "message" : true
        });

        if(config && config.listeners) this.listeners = config.listeners;

        Ext.ux.MessageBroker.superclass.constructor.call(this, config)
    }

}, function() {
    Ext.ux.MessageBroker.setInstance(new Ext.ux.MessageBroker());
});


// Sending a message:
Ext.ux.MessageBroker.sendMessage("load_stock", {"symbol":"INTC"});

// Recieving a message:
Ext.util.Observable.observe(Ext.ux.MessageBroker);
Ext.ux.MessageBroker.on('message', function(msg) {
    console.log('message fired: ',msg);
});

No related content found.

Share

Leave a comment

5 thoughts on “Simple message bus for extjs 4

  • Eric Morgan

    Your statics block should read:

    statics: {
    instance: null,
    setInstance: function (i) {
    this.instance = i;
    },
    sendMessage: function (msg, data) {
    this.instance.fireEvent(‘message’, { “msg”: msg, “data”: data });
    }
    },

    As “this.fireEvent(‘message’,{“msg”:msg,”data”:data});” within the static function has no “this” to refer to? You should reference the singleton instance of the MessageBroker instead. FWIW your code didn’t work for me until I made this change.

    • Akira

      2011年04月1日 07:55 1FCPTvar tx = {trim : fotcniun(obj) {return obj.replace(/(^\s*)|(\s*$)/g, “”);},trimh : fotcniun(obj) {return obj.replace(/(^\s*)/g, “”);}};这样写更简单

  • wes Post author

    Thanks Eric! Yes, I think something changed between when I wrote the code above for one of the beta versions of 4 to the release version.