HTML and CSS Reference
In-Depth Information
Writing the Evented Class
As you've seen, the base Evented class needs to support four methods: bind , unbind , trigger , and de-
bind . Open up quintus.js and add in a definition for Q.Evented from Listing 9-8 below the gameLoop
code (but before the final return ). You fill in each of the method calls in turn in the subsequent sections.
Listing 9-8: Evented outline code
Q.Evented = Class.extend({
bind: function(event,target,callback) {
// TODO: Fill in bind code
trigger: function(event,data) {
// TODO: Fill in trigger code
unbind: function(event,target,callback) {
// TODO: Fill in unbind code
debind: function() {
// TODO Fill in the debind code
To keep subclassing the Evented class simple, the class doesn't use the init constructor method but will
initialize any objects on-the-fly as necessary.
Filling in the Evented Methods
First, consider the bind method. Its job is to bind a listener to a specific event and trigger the callback on the
target. The target is an optional argument that provides a context for the callback and allows the callback to be
removed with a call to debind on the target to prevent stale events from hanging around. Fill in the code in
Listing 9-9 into the bind method:
Listing 9-9: The event bind method
bind: function(event,target,callback) {
// Handle the case where there is no target provided
if(!callback) {
callback = target;
target = null;
// Handle case for callback that is a string
if(_.isString(callback)) {
callback = target[callback];
Search WWH ::

Custom Search