mrpx
mrpx

Reputation: 95

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.

Upvotes: 6

Views: 2735

Answers (1)

atdc12
atdc12

Reputation: 56

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();

Upvotes: 3

Related Questions