API Reference

Class List

pc.events

Extend any normal object with events

var o = {};
o = pc.extend(o, pc.events);

// attach event
o.on("event_name", function() {
  alert('event_name fired');
}, this);

// fire event
o.fire("event_name");

// detach all events from object
o.off('event_name');

Summary

Static Methods

attachAttach event methods 'on', 'off', 'fire' and 'hasEvent' to the target object
fireFire an event, all additional arguments are passed on to the event listener
hasEventTest if there are any handlers bound to an event name
offDetach an event handler from an event.
onAttach an event handler to an event
onceAttach an event handler to an event.

Details

Static Methods

attach(target)

Attach event methods 'on', 'off', 'fire' and 'hasEvent' to the target object

Parameters

targetObjectThe object to add events to.

Returns

Object The target object

fire(name)

Fire an event, all additional arguments are passed on to the event listener

var o = {};
pc.events.attach(o);
o.on('event_name', function (msg) {
  alert('event_name fired: ' + msg);
});
o.fire('event_name', 'This is the message');

Parameters

nameObjectName of event to fire
...*Arguments that are passed to the event handler

hasEvent(name)

Test if there are any handlers bound to an event name

var o = {};
pc.events.attach(o); // add events to o
o.on('event_name', function () {}); // bind an event to 'event_name'
o.hasEvent('event_name'); // returns true

Parameters

nameStringThe name of the event to test

off(name, [callback], [scope])

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.

var handler = function () {
};
var o = {};
pc.events.attach(o);
o.on('event_name', handler);

o.off('event_name'); // Remove all events called 'event_name'
o.off('event_name', handler); // Remove all handler functions, called 'event_name'
o.off('event_name', handler, this); // Remove all hander functions, called 'event_name' with scope this

Parameters

nameStringName of the event to unbind
callbackfunctionFunction to be unbound
scopeObjectScope that was used as the this when the event is fired

on(name, callback, [scope])

Attach an event handler to an event

var o = {};
pc.events.attach(o);
o.on('event_name', function (a, b) {
  console.log(a + b);
});
o.fire('event_name', 1, 2); // prints 3 to the console

Parameters

nameStringName of the event to bind the callback to
callbackfunctionFunction that is called when event is fired. Note the callback is limited to 8 arguments.
scopeObjectObject to use as 'this' when the event is fired, defaults to current this

once(name, callback, [scope])

Attach an event handler to an event. This handler will be removed after being fired once.

var o = {};
pc.events.attach(o);
o.once('event_name', function (a, b) {
  console.log(a + b);
});
o.fire('event_name', 1, 2); // prints 3 to the console
o.fire('event_name', 1, 2); // not going to get handled

Parameters

nameStringName of the event to bind the callback to
callbackfunctionFunction that is called when event is fired. Note the callback is limited to 8 arguments.
scopeObjectObject to use as 'this' when the event is fired, defaults to current this