HTML and CSS Reference
In-Depth Information
if(this.listeners[event]) {
delete this.listeners[event];
}
} else {
var l = this.listeners && this.listeners[event];
if(l) {
for(var i = l.length-1;i>=0;i--) {
if(l[i][0] == target) {
if(!callback || callback == l[i][1]) {
this.listeners[event].splice(i,1);
}
}
}
}
}
},
The unbind method can take one, two, or three parameters, each providing more specificity for the exact
event to be removed. In the first case, where no target is provided, the object removes the entire list of listeners
for that event by simply removing the key from the object.
In the second and third cases, where you are unbinding only one or a few of all the possible listeners to that
event, the method needs to actually loop through each of the listeners and remove the ones that are being un-
bound using the built-in Array.splice method. Looping over an array and removing elements from it at the
same time is a somewhat tricky proposition because if you loop over the array normally and then change its
length, you can end up with a problem. One way around this is to loop from the end of the array on down to the
beginning. If an element needs to be removed, it won't affect the index of elements previous to it in the array.
The last method on Evented is the debind method, which removes an object from any listeners it's re-
gistered with. unbind is used to remove listeners from an object, while debind is used when an object is
being destroyed to remove all of its listeners to prevent memory leaks and unexpected behavior. Fill in the code
for debind from Listing 9-12 .
Listing 9-12: Evented debind method
debind: function() {
if(this.binds) {
for(var i=0,len=this.binds.length;i<len;i++) {
var boundEvent = this.binds[i],
source = boundEvent[0],
event = boundEvent[1];
source.unbind(event,this);
}
}
}
This code loops over each of the elements in the this.binds array and calls unbind to remove them.
 
 
Search WWH ::




Custom Search