getEvent()

The getEvent() function (client-side) retrieves a custom event object by its name. It also lets you attach listeners and resolve or reject events. By working with event objects, you can synchronize custom client-side logic and build responsive behaviors in your scripts.

Syntax

Use this syntax for the getEvent() function:

            var event = getEvent('eventName'); 

          

Return Value

The getEvent() function returns an object representing the specified event. This object includes the name property, which corresponds to the event name provided to the function. The object also exposes several methods to handle event resolution, failure, and listeners.

Method

Description

done(callback)

Invokes the callback when the event is resolved.

fail(callback)

Registers the callback for failure events.

reject(data)

Marks the event as failed and passes data to listeners.

resolve(data)

Marks the event as successful and passes data to listeners.

resolveSpecial(options)

If the verified condition returns true, this method resolves the event and applies the specified options.

then(callback)

Runs the callback after resolution or rejection of the event.

Parameters

The getEvent() function accepts the event name in string format as a required parameter.

Examples

The following examples show how to use the getEvent() function.

Attaching a Listener to an Event

This example attaches a callback to an event named floor.ready. The callback logs a message when the event is resolved by any another part of the code.

              var event = getEvent('floor.ready');
event.then(function(data) {
    console.log('Floor is ready.');
}); 

            

Resolving an Event When a Condition Is Met

This example uses resolveSpecial() to verify a condition. The method checks every 10 milliseconds for up to 1,000 milliseconds to verify whether the element exists. If the condition is met, the method resolves the event and passes the value into listeners. The event is resolved when the function returns true or stops when the timeout is reached.

              var event = getEvent('draw');
event.resolveSpecial({
    func: function() {
        return jQuery('#canvas01')[0];
    },
    time: 1000,
    delay: 10,
    data: 'canvas rendered'
}); 

            

Related Topics

General Notices