Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dispatching custom events with Dojo framework

I'm using Dojo framework to help me in my Javascript development with cross browsing DOM manipulation and event managing.
For this last I was hoping to use custom event dispatching between objects. But I don't find anything on this. I read about subscribe/publish, but it not exactly what I want.
Here is what I'd want to do :

var myObject = new CustomObject();
dojo.connect(myObject, 'onCustomEvent', function(argument) {
    console.log('custom event fired with argument : ' + argument);
});


var CustomObject = (function() {
    CustomObject = function() {
        // Something which should look like this
        dojo.dispatch(this, 'onCustomEvent', argument);
    };
}) ();

Anyone could help me ?

Thanks.

like image 783
mrpx Avatar asked Nov 02 '11 10:11

mrpx


1 Answers

I normally do it this way: (tested with Dojo 1.3.2)

dojo.declare("CustomObject", null, {
    randomFunction: function() {
        // do some processing

        // raise event
        this.onCustomEvent('Random Argument');
    },

    onCustomEvent: function(arg) {
    }
});

var myObject = new CustomObject();
dojo.connect(myObject, 'onCustomEvent', function(argument) {
    console.log('custom event fired with argument : ' + argument);
});


// invoke the function which will raise the custom event
myObject.randomFunction();
like image 173
Aswin Trisnadi Avatar answered Oct 06 '22 00:10

Aswin Trisnadi