我想添加一个通用对话框,其中"Ok"和"Cancel"按钮支持回调函数.
如何使用Dojo AMD实现这一目标?
例如:
myDialog = new Dialog ({
title: "My Dialog",
content: "Are you sure, you want to delete the selected Record?",
style: "width: 300px",
onExecute:function(){ //Callback function
console.log("Record Deleted")
},
onCancel:function(){
console.log("Event Cancelled") }
});
// create a button to clear the cart
new Button({ label:"Ok"
onClick: myDialog.onExecute()
}
new Button({ label:"Cancel"
onClick: function(){ myDialog.onCancel() }
}
Run Code Online (Sandbox Code Playgroud)
phu*_*ick 29
这是我遇到同样问题时提出的解决方案.它不是完全程序化的,但使用模板使代码更易读,更灵活.
最好只看一次,而不是听100次,所以看下面的所有代码住在jsFiddle:http://jsfiddle.net/phusick/wkydY/
我采用的主要原则是dijit.Dialog::content不仅可以是字符串,还可以是小部件实例.所以我将子类dijit.Dialog声明为ConfirmDialogclass.在ConfirmDialog::constuctor()我宣布和instantize从模板窗口小部件,并将其设置为对话框的内容.然后我onClick在ConfirmDialog::postCreate()方法中连接动作:
var ConfirmDialog = declare(Dialog, {
title: "Confirm",
message: "Are you sure?",
constructor: function(/*Object*/ kwArgs) {
lang.mixin(this, kwArgs);
var message = this.message;
var contentWidget = new (declare([_Widget, _TemplatedMixin, _WidgetsInTemplateMixin], {
templateString: template, //get template via dojo loader or so
message: message
}));
contentWidget.startup();
this.content = contentWidget;
},
postCreate: function() {
this.inherited(arguments);
this.connect(this.content.cancelButton, "onClick", "onCancel");
}
})
Run Code Online (Sandbox Code Playgroud)
模板标记:
<div style="width:300px;">
<div class="dijitDialogPaneContentArea">
<div data-dojo-attach-point="contentNode">
${message}
</div>
</div>
<div class="dijitDialogPaneActionBar">
<button
data-dojo-type="dijit.form.Button"
data-dojo-attach-point="submitButton"
type="submit"
>
OK
</button>
<button
data-dojo-type="dijit.form.Button"
data-dojo-attach-point="cancelButton"
>
Cancel
</button>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
现在使用ConfirmDialog而不是dijit.Dialog:
var confirmDialog = new ConfirmDialog({ message: "Your message..."});
confirmDialog.show();
Run Code Online (Sandbox Code Playgroud)
重要提示:关闭时不要忘记断开对话框回调和销毁对话框的任何连接.
如果您ConfirmDialog经常在代码的多个位置使用,请考虑以下事项:
var MessageBox = {};
MessageBox.confirm = function(kwArgs) {
var confirmDialog = new ConfirmDialog(kwArgs);
confirmDialog.startup();
var deferred = new Deferred();
var signal, signals = [];
var destroyDialog = function() {
array.forEach(signals, function(signal) {
signal.remove();
});
delete signals;
confirmDialog.destroyRecursive();
}
signal = aspect.after(confirmDialog, "onExecute", function() {
destroyDialog();
deferred.resolve('MessageBox.OK');
});
signals.push(signal);
signal = aspect.after(confirmDialog, "onCancel", function() {
destroyDialog();
deferred.reject('MessageBox.Cancel');
});
signals.push(signal);
confirmDialog.show();
return deferred;
}
Run Code Online (Sandbox Code Playgroud)
您的代码将更具可读性,您无需处理清理:
MessageBox.confirm().then(function() {
console.log("MessageBox.OK")
});
Run Code Online (Sandbox Code Playgroud)
Eze*_*aga 11
以下是如何使用dijit/ConfirmDialog和配置其按钮.
require(["dijit/ConfirmDialog"], function(ConfirmDialog){
// create instance
var dialog = new ConfirmDialog({
title: "Session Expiration",
content: "the test. Your session is about to expire. Do you want to continue?",
style: "width: 300px"
});
// change button labels
dialog.set("buttonOk","Yes");
dialog.set("buttonCancel","No");
// register events
dialog.on('execute', function() { /*do something*/ });
dialog.on('cancel', function() { /*do something*/ });
// show
dialog.show();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24384 次 |
| 最近记录: |