Custom events are used by all jQuery UI plugins as a means of communicating when something has happened. For convenience we also allow a callback to be passed in the options hash instead of binding an event listener.
All events triggered by a plugin are prefixed, e.g., dialog's open event is "dialogopen" and has a related callback option with the name "open". So the following two blocks of code accomplish the same thing:
$(el).dialog({ open: function() { alert( "dialog was opened" ); } });
$(el).dialog().bind( "dialogopen", function() { alert( "dialog was opened" ); });
The base widget provides a ._trigger() method to accomplish this. So inside the dialog plugin if you wanted to trigger the open callback/event, you would do:
this._trigger( "open" );
The ._trigger() method takes three parameters:
- callback name - required - name of the callback to trigger. The event name is generated by concatenating the plugin name and the callback name.
- original event - optional - if this custom event is being triggered by some other event, e.g., a click event, you can pass that along so that the information will be copied over to the new event.
- ui/data object - optional - if there is extra information relevant to the event you can pass it as a hash and that hash will be passed to the callback and any event listeners.