createEvent()
The createEvent() function (client-side) creates a custom event that helps different actions coordinate asynchronous steps without having to call each other directly. You can create the event in one action, attach callbacks in the same or another action, and complete it later by calling resolve() or reject(). This function is useful when your implementation flow depends on timing-like waiting for a user interface element, an external script, or another action to finish.
The createEvent() function works together with getEvent() to retrieve the event and trigger it when the condition is met. Use getEvents() when you need to work with multiple events at the same time.
Syntax
Use this syntax for the createEvent() function:
createEvent('eventName');
Return Value
The createEvent() function returns an event object. You can use this object to register callbacks and control when the event is completed. The following table outlines the methods supported by the returned event object.
|
Method |
Description |
|---|---|
|
Registers a success callback. |
|
Registers a failure callback. |
|
Registers a callback that runs after either success or failure.
Note:
|
|
Marks the event as successful and passes data to callbacks.
Note:
|
|
Marks the event as failed and passes data to callbacks.
Note:
|
|
Resolves the event when a custom condition is met. |
For the resolveSpecial() method, use these properties for the object taken as a parameter:
-
func- Runs this function until it returnstrue. -
time- Sets the total wait duration in milliseconds. The default value is5000. -
delay- Sets the interval between checks in milliseconds. The default value is100. -
data- Passes this information to the callbacks when the event resolves.
Parameters
The createEvent() function accepts the event name in string format as a required parameter.
Examples
The following examples show how to use the createEvent() function.
Creating an Event
This example creates an event called connect.
createEvent('connect');
Retrieving an Existing Event Object
This example retrieves a previously created event.
var event = getEvent('connect');
Creating and Resolving an Event With Data
For example, you can start by creating an event.
createEvent('myEvent');
In another action or later in the code, you can retrieve the event and register success and failure callback.
var event = getEvent('myEvent');
event.done(function(data) {
console.log('Event resolved with:', data);
});
event.fail(function(err) {
console.log('Event rejected with:', err);
});
Lastly, you can resolve the event when appropriate.
event.resolve({ success: true });
Resolving an Event When a Condition Is Met
This example uses resolveSpecial() to verify a condition. The event is resolved when the function returns true or stops when the timeout is reached.
createEvent('draw');
var event = getEvent('draw');
event.done(function(data) {
console.log('Ready:', data);
});
event.resolveSpecial({
func: function() {
return jQuery('#canvas01')[0];
},
time: 1000,
delay: 10,
data: 'canvas rendered'
});