Bug Tracker

Changeset 5352

Show
Ignore:
Timestamp:
04/29/08 22:06:54 (7 months ago)
Author:
aflesler
Message:

jquery event: from #2249, adding $.event.proxy to link event handlers, and implementing it on $.event.add, $.fn._toggle and $.fn.one.
It also fixes a bug in $.fn.one that was unbinding ALL the existing handlers.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/jquery/src/event.js

    r5351 r5352  
    2727 
    2828            // Create unique handler function, wrapped around original handler  
    29             handler = function() {  
     29            handler = this.proxy( fn, function() {  
    3030                // Pass arguments and context to original handler  
    3131                return fn.apply(this, arguments);  
    32             }; 
     32            }); 
    3333 
    3434            // Store data in unique handler  
     
    347347    }, 
    348348     
     349    proxy: function( fn, proxy ){ 
     350        // Set the guid of unique handler to the same of original handler, so it can be removed  
     351        proxy.guid = fn.guid = fn.guid || proxy.guid || this.guid++; 
     352        return proxy;//so proxy can be declared as an argument 
     353    }, 
     354     
    349355    special: { 
    350356        ready: { 
     
    412418     
    413419    one: function( type, data, fn ) { 
     420        var one = jQuery.event.proxy( fn || data, function(event) { 
     421            jQuery(this).unbind(event, one); 
     422            return (fn || data).apply( this, arguments ); 
     423        }); 
    414424        return this.each(function(){ 
    415             jQuery.event.add( this, type, function(event) { 
    416                 jQuery(this).unbind(event); 
    417                 return (fn || data).apply( this, arguments); 
    418             }, fn && data); 
     425            jQuery.event.add( this, type, one, fn && data); 
    419426        }); 
    420427    }, 
     
    438445    }, 
    439446 
    440     toggle: function() { 
     447    toggle: function( fn ) { 
    441448        // Save reference to arguments for access in closure 
    442         var args = arguments; 
    443  
    444         return this.click(function(event) { 
     449        var args = arguments, i = 1; 
     450 
     451        // link all the functions, so any of them can unbind this click handler 
     452        while( i < args.length ) 
     453            jQuery.event.proxy( fn, args[i++] ); 
     454 
     455        return this.click( jQuery.event.proxy( fn, function(event) { 
    445456            // Figure out which function to execute 
    446             this.lastToggle = ( this.lastToggle || 0 ) % args.length; 
     457            this.lastToggle = ( this.lastToggle || 0 ) % i; 
    447458             
    448459            // Make sure that clicks stop 
     
    451462            // and execute the function 
    452463            return args[ this.lastToggle++ ].apply( this, arguments ) || false; 
    453         }); 
     464        })); 
    454465    }, 
    455466