Callbacks
Callbacks are (usually anonymous) functions which are assigned to a variable or property and are called when a certain event occurs. js.Dialog makes extensive use of this technique. In fact, most dialogs are not very usefull without it.
Different to JavaScript’s built-in dialog functions, js.Dialog’s dialogs are asynchronous; i.e. control is passed back to the calling script immediately after the dialog is opened. If you want to run code after the dialog is closed, it has to go into the appropriate callback.
Dialog.core
- onLoad
- Will be called when the dialog has finished loading. It receives a reference to the dialog instance as a parameter.
- Parameters: dlg: Dialog.core
- Note: Dialog.simple sets this callback to a function which activates the dialog form. If you override this function, please make sure to activate the dialog form.
- onButtonClick
- This will be called when one of the buttons on the form was clicked. These buttons should be numbered, and the number of the button is given as second parameter.
- Parameters: dlg: Dialog.core, num: Integer
- Note: Dialog.simple sets this callback to a function which calls dlg.cancel(), dlg.confirm() or dlg.otherButton(), depending on the button pressed. If you override that function, you need to replicate this functionality or the onCancel, onConfirm and onOtherButton events will not fire.
Dialog.simple
- onCancel
- Will be called when the dialog is cancelled. Please note that this could happen not only by clicking on the Cancel-button, but also if the user clicks in the closebox or (if the option cancelWhenOverlayIsClicked is active) by clicking in the overlay area.
- Parameters: dlg: Dialog.core
- Note: If you return false from the callback function, the dialog will not be closed. Return true, or undefined (i.e. don't return anything) and the dialog will be closed after the callback is finished.
- onConfirm
- Will be called when the dialog is confirmed. This usually happens if the user clicks the default button (button1).
- Parameters: dlg: Dialog.core
- Note: If you return false from the callback function, the dialog will not be closed. Return true, or undefined (i.e. don't return anything) and the dialog will be closed after the callback is finished.
- onOtherButton
- Will be called when the tertiary (or "other") button is clicked.
- Parameters: dlg: Dialog.core
- Note: The dialog is not automatically closed after this callback has finished.
Examples
- Confirm
- This is a simple example for a customized Dialog.confirm dialog with custom callback functions:
var dlg = Dialog.confirm('Say, yes or no?', { onCancel: function(dlg) { alert('You cancelled!'); }, onConfirm: function(dlg) { alert('Okey dokey!'); } });
- Prompt
- In Dialog.prompt-dialogs, you need to use a callback if you actually want to read the value the user entered. For example like this:
var dlg = Dialog.prompt('What was your name again?', { input: 'anonymous', onConfirm: function(dlg) { var name = dlg.getText(); Dialog.alert('So your name is ' + name + ', right?'); } });- Two things you should note here: first, we use the variable
dlgto get a reference to the dialog object, and secondly the functiongetText()to get the actual value of the text field.