Class glow.events.Target
Defined in: core.js.
| Constructor Attributes | Constructor Name and Description |
|---|---|
|
An object that can have event listeners and fire events.
|
| Method Attributes | Method Name and Description |
|---|---|
|
detach(eventName, callback, thisVal)
Remove an event listener.
|
|
| <static> |
glow.events.Target.extend(obj)
Convenience method to add Target instance methods onto an object.
|
|
fire(eventName, event)
Fire an event.
|
|
|
on(eventName, callback, thisVal)
Listen for an event
|
Class Detail
glow.events.Target()
An object that can have event listeners and fire events.
Extend this class to make your own objects have 'on' and 'fire'
methods.
// Ball is our constructor
function Ball() {
// ...
}
// make Ball inherit from Target
glow.util.extend(Ball, glow.events.Target, {
// additional methods for Ball here, eg:
bowl: function() {
// ...
}
});
// now instances of Ball can receive event listeners
var myBall = new Ball();
myBall.on('bounce', function() {
alert('BOING!');
});
// and events can be fired from Ball instances
myBall.fire('bounce');
Method Detail
detach(eventName, callback, thisVal)
Remove an event listener.
function showListener() {
// ...
}
// add listener
myObj.on('show', showListener);
// remove listener
myObj.detach('show', showListener);
// note the following WILL NOT WORK
// add listener
myObj.on('show', function() {
alert('hi');
});
// remove listener
myObj.detach('show', function() {
alert('hi');
});
// this is because both callbacks are different function instances
- Parameters:
- {string} eventName
- Name of the event to remove.
- {function} callback
- Callback to detach.
- {Object} thisVal Optional
- Value of 'this' within the callback. By default, this is the object being listened to.
- Returns:
- this Target object
<static>
glow.events.Target.extend(obj)
Convenience method to add Target instance methods onto an object.
If you want to add Target methods to a class, extend glow.events.Target instead.
var myApplication = {};
glow.events.Target.extend(myApplication);
// now myApplication can fire events...
myApplication.fire('load');
// and other objects can listen for those events
myApplication.on('load', function(e) {
alert('App loaded');
});
- Parameters:
- {Object} obj
- Object to add Target instance methods to.
fire(eventName, event)
Fire an event.
myObj.fire('show');
// adding properties to the event object
myBall.fire('bounce', {
velocity: 30
});
// BallBounceEvent extends glow.events.Event but has extra methods myBall.fire( 'bounce', new BallBounceEvent(myBall) );
- Parameters:
- {string} eventName
- Name of the event to fire.
- {glow.events.Event|Object} event Optional
- Event object to pass into listeners. You can provide a simple object of key-value pairs which will be added as properties of a glow.events.Event instance.
- Returns:
- glow.events.Event
on(eventName, callback, thisVal)
Listen for an event
myObj.on('show', function() {
// do stuff
});
- Parameters:
- {string} eventName
- Name of the event to listen for.
- {function} callback
- Function to call when the event fires. The callback is passed a single event object. The type of this object depends on the event (see documentation for the event you're listening to).
- {Object} thisVal Optional
- Value of 'this' within the callback. By default, this is the object being listened to.
- Returns:
- this