// Remember the function in a global list (for triggering)
if (!this.global[type])
this.global[type] = [];
- this.global[type].push( element );
+ // Only add the element to the global list once
+ if (jQuery.inArray(element, this.global[type]) == -1)
+ this.global[type].push( element );
},
guid: 1,
// Detach an event or set of events from an element
remove: function(element, type, handler) {
- var events = element.$events, ret;
+ var events = element.$events, ret, index;
if ( events ) {
// type is actually an event object here
element.detachEvent("on" + type, element.$handle);
ret = null;
delete events[type];
+
+ // Remove element from the global event type cache
+ while ( this.global[type] && ( (index = jQuery.inArray(element, this.global[type])) >= 0 ) )
+ delete this.global[type][index];
}
}
* data as the second parameter (and the handler function as the third), see
* second example.
*
+ * Calling bind with an event type of "unload" will automatically
+ * use the one method instead of bind to prevent memory leaks.
+ *
* @example $("p").bind("click", function(){
* alert( $(this).text() );
* });
* @cat Events
*/
bind: function( type, data, fn ) {
- return this.each(function(){
+ return type == "unload" ? this.one(type, data, fn) : this.each(function(){
jQuery.event.add( this, type, fn || data, fn && data );
});
},