I would like to add a generic dialog with “Ok” and “Cancel” buttons support callback functions.
How can I achieve this with a Dojo AMD?
For example:
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() }
}
Here is the solution I came up when I had been facing the same question. It’s not completely programmatic, but using the template makes the code more readable and flexible for changes.
Better to see once than to hear 100 times, so see all the code below live at jsFiddle: http://jsfiddle.net/phusick/wkydY/
The main principle I employ is the fact that
dijit.Dialog::contentmay not only be a string, but also a widget instance. So I subclassdijit.Dialogto declareConfirmDialogclass. InConfirmDialog::constuctor()I declare and instantize a widget from a template and set it to be dialog’s content. Then I wireonClickactions inConfirmDialog::postCreate()method:The template markup:
Now use
ConfirmDialoginstead ofdijit.Dialog:Important: Do not forget to disconnect any connects to the dialogs callbacks and destroy dialog when closed.
If you use
ConfirmDialogoften and in multiple places of your code consider this:Your code will be more readable and you will not have to deal with cleaning up: