我正在使用 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();
关于javascript - 使用 Dojo 框架调度自定义事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7978898/