使用Dojo框架分派自定义事件

6

我正在使用Dojo框架来帮助我进行跨浏览器DOM操作和事件管理的JavaScript开发。
对于后者,我希望在对象之间使用自定义事件分派。但是我没有找到相关信息。我了解了subscribe/publish,但它不完全符合我的要求。
这是我想要做的:

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

有人能帮我吗?

谢谢。

1个回答

3
我通常这样做:(在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();

谢谢。所以你必须将一个现有的函数作为事件标识符进行调用。我更习惯于使用字符串+处理程序系统,但我会处理好的。 再次感谢。 - mrpx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接