我正在使用Dojo框架通过交叉浏览DOM操作和事件管理来帮助我进行Javascript开发。
最后,我希望在对象之间使用自定义事件分配。但是我没有找到任何东西。我读到有关订阅/发布的信息,但这并不是我想要的。
这是我想要做的:

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

有人可以帮助我吗?

谢谢。

最佳答案

我通常是这样进行的:(已在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();

07-24 09:44
查看更多