Namespace for event functions. Use these functions to attach events to an object.
- Source:
Example
var obj = { };
pc.events.attach(obj);
// subscribe to an event
obj.on('hello', function(str) {
console.log('event hello is fired', str);
});
// fire event
obj.fire('hello', 'world');
Methods
(static) attach(target) → {Object}
Attach event methods 'on', 'off', 'fire', 'once' and 'hasEvent' to the target object
Parameters:
Name | Type | Description |
---|---|---|
target |
Object | The object to add events to. |
- Source:
Returns:
The target object
- Type
- Object
Example
var obj = { };
pc.events.attach(obj);
(static) fire(name) → {*}
Fire an event, all additional arguments are passed on to the event listener
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
Object | Name of event to fire | |
... |
* |
<optional> |
Arguments that are passed to the event handler |
- Source:
Returns:
'this' for chaining
- Type
- *
Example
obj.fire('test', 'This is the message');
(static) hasEvent(name) → {Boolean}
Test if there are any handlers bound to an event name
Parameters:
Name | Type | Description |
---|---|---|
name |
String | The name of the event to test |
- Source:
Returns:
true if the object has handlers bound to the specified event name.
- Type
- Boolean
Example
obj.on('test', function () { }); // bind an event to 'test'
obj.hasEvent('test'); // returns true
obj.hasEvent('hello'); // returns false
(static) off(nameopt, callbackopt, scopeopt) → {*}
Detach an event handler from an event. If callback is not provided then all callbacks are unbound from the event,
if scope is not provided then all events with the callback will be unbound.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
String |
<optional> |
Name of the event to unbind |
callback |
function |
<optional> |
Function to be unbound |
scope |
Object |
<optional> |
Scope that was used as the this when the event is fired |
- Source:
Returns:
'this' for chaining
- Type
- *
Example
var handler = function () {
};
obj.on('test', handler);
obj.off(); // Removes all events
obj.off('test'); // Removes all events called 'test'
obj.off('test', handler); // Removes all handler functions, called 'test'
obj.off('test', handler, this); // Removes all hander functions, called 'test' with scope this
(static) on(name, callback, scopeopt) → {*}
Attach an event handler to an event
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
String | Name of the event to bind the callback to | |
callback |
function | Function that is called when event is fired. Note the callback is limited to 8 arguments. | |
scope |
Object |
<optional> |
Object to use as 'this' when the event is fired, defaults to current this |
- Source:
Returns:
'this' for chaining
- Type
- *
Example
obj.on('test', function (a, b) {
console.log(a + b);
});
obj.fire('test', 1, 2); // prints 3 to the console
(static) once(name, callback, scopeopt) → {*}
Attach an event handler to an event. This handler will be removed after being fired once.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
String | Name of the event to bind the callback to | |
callback |
function | Function that is called when event is fired. Note the callback is limited to 8 arguments. | |
scope |
Object |
<optional> |
Object to use as 'this' when the event is fired, defaults to current this |
- Source:
Returns:
'this' for chaining
- Type
- *
Example
obj.once('test', function (a, b) {
console.log(a + b);
});
obj.fire('test', 1, 2); // prints 3 to the console
obj.fire('test', 1, 2); // not going to get handled