在 Dojo 的确认对话框中取消注册执行操作
Unregistering On-Execute Action on a Confirm Dialog in Dojo
我有一个 dojo 小部件如下:
define("myWidget/AnExperiment", [
"dojo/_base/declare",
"dojo/dom-style",
"dijit/ConfirmDialog",
"dijit/_WidgetBase"
], function(declare, domStyle, ConfirmDialog, _WidgetBase) {
return declare([_WidgetBase], {
myDialog: null,
createDialog: function() {
self = this;
self.myDialog = new ConfirmDialog({
title: 'MyDialog',
content: 'This is my Dialog.',
closable: false
});
domStyle.set(myDialog.cancelButton.domNode, "display", "none");
self.myDialog.on("execute", function() {
console.log('Func1 called');
self.myFunc1();
});
self.myDialog.show();
setTimeout(function() {
self.changeDialogAction();
}, 30000);
}
changeDialogAction: function () {
var self = this;
self.myDialog.set("title", 'Changed MyDialog');
self.myDialog.set("content", 'This is the changed dialog');
// WHAT DO I DO HERE TO UN-REGISTER OLD ON-EXECUTE ACTION
self.myDialog.on("execute", function() {
console.log('Func2 called');
self.myFunc2();
});
},
myFunc1: function() {
console.log('This is func1');
},
myFunc2: function() {
console.log('This is func2');
}
}
调用 createDialog
时,会创建一个 ConfirmDialog
。它最初配置为调用 Func1
on-execute
。然后显示 Dialog
。
但是,如果用户在 30 秒内未执行任何操作,on-execute
操作将更改为调用 Func2
。
我的问题是,当用户在 30 秒后单击时,不仅会调用 Func2
,还会调用 Func1
和 Func2
。
如何取消注册,以便在 30 秒后执行时仅调用 Func2
?
来自文档"The return value of on()
provides a method that can be used to remove the event listener from the event."
例如:
require(["dojo/on", "dojo/_base/window"], function(on, win){
var signal = on(win.doc, "click", function(){
// remove listener after first event
signal.remove();
// do something else...
});
});
所以一旦 function1 被调用,您可以使用处理程序(信号)将其删除。
我有一个 dojo 小部件如下:
define("myWidget/AnExperiment", [
"dojo/_base/declare",
"dojo/dom-style",
"dijit/ConfirmDialog",
"dijit/_WidgetBase"
], function(declare, domStyle, ConfirmDialog, _WidgetBase) {
return declare([_WidgetBase], {
myDialog: null,
createDialog: function() {
self = this;
self.myDialog = new ConfirmDialog({
title: 'MyDialog',
content: 'This is my Dialog.',
closable: false
});
domStyle.set(myDialog.cancelButton.domNode, "display", "none");
self.myDialog.on("execute", function() {
console.log('Func1 called');
self.myFunc1();
});
self.myDialog.show();
setTimeout(function() {
self.changeDialogAction();
}, 30000);
}
changeDialogAction: function () {
var self = this;
self.myDialog.set("title", 'Changed MyDialog');
self.myDialog.set("content", 'This is the changed dialog');
// WHAT DO I DO HERE TO UN-REGISTER OLD ON-EXECUTE ACTION
self.myDialog.on("execute", function() {
console.log('Func2 called');
self.myFunc2();
});
},
myFunc1: function() {
console.log('This is func1');
},
myFunc2: function() {
console.log('This is func2');
}
}
调用 createDialog
时,会创建一个 ConfirmDialog
。它最初配置为调用 Func1
on-execute
。然后显示 Dialog
。
但是,如果用户在 30 秒内未执行任何操作,on-execute
操作将更改为调用 Func2
。
我的问题是,当用户在 30 秒后单击时,不仅会调用 Func2
,还会调用 Func1
和 Func2
。
如何取消注册,以便在 30 秒后执行时仅调用 Func2
?
来自文档"The return value of on()
provides a method that can be used to remove the event listener from the event."
例如:
require(["dojo/on", "dojo/_base/window"], function(on, win){
var signal = on(win.doc, "click", function(){
// remove listener after first event
signal.remove();
// do something else...
});
});
所以一旦 function1 被调用,您可以使用处理程序(信号)将其删除。