【问题标题】:Dispatching custom events with Dojo framework使用 Dojo 框架调度自定义事件
【发布时间】:2011-11-02 10:32:42
【问题描述】:

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

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

谁能帮帮我?

谢谢。

【问题讨论】:

    标签: javascript events dojo


    【解决方案1】:

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

    【讨论】:

    • 谢谢。因此,您必须调用现有函数作为事件标识符。我更喜欢使用字符串 + 处理程序系统,但我会处理它。再次感谢
    猜你喜欢
    • 2015-02-14
    • 2011-11-12
    • 2010-10-03
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    相关资源
    最近更新 更多