pulp javascript Event System for Objects
Below are examples and use cases for the pulp event system for Objects.
The pulp event methods are available for all Objects created with pulp.cls.create().
The pulp event system, while inspired by the DOM event handling mechanism, is
distinct from and independent of DOM events. Indeed, this system
allows the developer to respond to events that aren't necessarily
initiated by direct user input like key presses or mouse-button
clicks. For example, pulp.ajax instances fire events such as
success and timeout.
- Introduction
- On and After
- Bubbling
- Defining event options
- Subscription events
- Debugging
top ^1. On and After
Example 1.1: Unique Event Firing Pattern
classInstance.fire(eventName, defaultAction, relatedData);
Example 1.2: Firing an Event
var myClass = pulp.cls.create({
finish: function(arg) {
this.fire('finish', function(event) {
// finish up something
}, {relatedData1: 'hello', relatedData2: 'world', arg: arg});
}
});
Example 1.3: Subscribing to an Event
var myClass = pulp.cls.create();
var myInstance = new myClass();
myInstance.on('finish', function(event) {
// do something before the finish action
event.type == 'finish';
event.target == this;
event.timing == 'on';
});
myInstance.after('finish', function(event) {
// do something else after the finish action
event.type == 'finish';
event.target == this;
event.timing == 'after';
});
Example 2.4: Subscribing to an Event through options object
var myClass = pulp.cls.create({
initialize: function(options) {
}
});
var myInstance = new myClass();
myInstance.on('finish', function(event) {
// do something before the finish action
event.type == 'finish';
event.target == this;
event.timing == 'on';
});
myInstance.after('finish', function(event) {
// do something else after the finish action
event.type == 'finish';
event.target == this;
event.timing == 'after';
});
top ^2. Bubbling
Example 2.1: event.preventDefault()
| Timing |
Effect |
Method Call |
| on |
fired |
event.preventDefault() |
| on |
fired |
|
| defaultAction |
not called |
|
| after |
not called |
|
| after |
not called |
|
Example 2.2: event.stopPropagation()
| Timing |
Effect |
Method Call |
| on |
fired |
event.stopPropagation() |
| on |
not called |
|
| defaultAction |
fired |
|
| after |
not called |
|
| after |
not called |
|
Example 2.3: event.halt()
| Timing |
Effect |
Method Call |
| on |
fired |
event.halt() |
| on |
not called |
|
| defaultAction |
not called |
|
| after |
not called |
|
| after |
not called |
|
Example 2.4a: event.stopImmediatePropagation()
| Timing |
Effect |
Method Call |
| on |
fired |
event.stopImmediatePropagation() |
| on |
not called |
|
| defaultAction |
fired |
|
| after |
fired |
|
| after |
fired |
|
Example 2.4b: event.stopImmediatePropagation()
| Timing |
Effect |
Method Call |
| on |
fired |
|
| on |
fired |
|
| defaultAction |
fired |
|
| after |
fired |
event.stopImmediatePropagation() |
| after |
not called |
|
Example 2.4b: event.stopImmediatePropagation()
| Timing |
Effect |
Method Call |
| on |
fired |
|
| on |
fired |
|
| defaultAction |
fired |
event.stopImmediatePropagation() |
| after |
fired |
|
| after |
fired |
|
top ^3. Defining Event Options
Example 3.1: Defining an Event
var myClass = pulp.cls.create({
initialize: function(arg) {
this.defineEvent('finish', {
// these are the default options
target: this, // the object that fired the event
bubbles: true, // propagation can be stopped
preventable: true, // default action can be prevented
scope: this, // scope of firing function
fireOnce: false // if true, fire only once
});
}
});
top ^4. Subscription Events
Example 4.1: Monitoring Subscribers
var myClass = pulp.cls.create({
initialize: function() {
this.on('event:subscribe', function(event) {
alert('I see you subscribing to ' + event.type);
});
}
});
var myInstance = new myClass();
myInstance.on('finish', myCallback);
// alerts "I see you subscribing to finish"
The methods can also be added to other objects or classes by copying all the properties within pulp.cls.event.mixin