]> source.dussan.org Git - jquery.git/commitdiff
Fixed and improved jQuery's event system: The target property of the event object...
authorJörn Zaefferer <joern.zaefferer@gmail.com>
Thu, 26 Oct 2006 12:41:25 +0000 (12:41 +0000)
committerJörn Zaefferer <joern.zaefferer@gmail.com>
Thu, 26 Oct 2006 12:41:25 +0000 (12:41 +0000)
src/event/event.js
src/jquery/jquery.js

index a60c4616aff29c624e66f61b69a65a84002bbeed..55e205ae2a6edc1d244eb576eae8a60f1f9f8f6c 100644 (file)
@@ -1583,8 +1583,8 @@ new function(){
                var o = e[i];
                
                // Handle event binding
-               jQuery.fn[o] = function(f){
-                       return f ? this.bind(o, f) : this.trigger(o);
+               jQuery.fn[o] = function(f, amount){
+                       return f ? this.bind(o, f, amount) : this.trigger(o);
                };
                
                // Handle event unbinding
@@ -1592,20 +1592,8 @@ new function(){
                
                // Finally, handle events that only fire once
                jQuery.fn["one"+o] = function(f){
-                       // Attach the event listener
-                       return this.each(function(){
-
-                               var count = 0;
-
-                               // Add the event
-                               jQuery.event.add( this, o, function(e){
-                                       // If this function has already been executed, stop
-                                       if ( count++ ) return true;
-                               
-                                       // And execute the bound function
-                                       return f.apply(this, [e]);
-                               });
-                       });
+                       // use bind with amount param to bind only once
+                       return this.bind(o, f, 1);
                };
                        
        };
index 9561ea9de4337e07cd775bf5d484ee56eb18977f..65f842381b32fcf61bb1d93fbc97accd0f9cb537 100644 (file)
@@ -2271,7 +2271,7 @@ jQuery.extend({
                handle: function(event) {
                        if ( typeof jQuery == "undefined" ) return false;
 
-                       event = event || jQuery.event.fix( window.event );
+                       event = jQuery.event.fix( event );
 
                        // If no correct event was found, fail
                        if ( !event ) return false;
@@ -2295,16 +2295,19 @@ jQuery.extend({
                },
 
                fix: function(event) {
-                       if ( event ) {
+                       if(jQuery.browser.msie) {
+                               event = window.event;
                                event.preventDefault = function() {
                                        this.returnValue = false;
                                };
-
                                event.stopPropagation = function() {
                                        this.cancelBubble = true;
                                };
+                               event.target = event.srcElement;
+                       } else if(jQuery.browser.safari && event.target.nodeType == 3) {
+                               event = jQuery.extend({}, event);
+                               event.target = event.target.parentNode;
                        }
-
                        return event;
                }
 
@@ -3396,15 +3399,29 @@ jQuery.macros = {
                 * } )
                 * @desc Stop only an event from bubbling by using the stopPropagation method.
                 *
+                * @example $("form").bind( "submit", function(event) {
+                *   // do something after submit
+                * }, 1 )
+                * @desc Executes the function only on the first submit event and removes it afterwards
+                *
                 * @name bind
                 * @type jQuery
                 * @param String type An event type
                 * @param Function fn A function to bind to the event on each of the set of matched elements
+                * @param Number amount An optional amount of times to execute the bound function
                 * @cat Events
                 */
-               bind: function( type, fn ) {
+               bind: function( type, fn, amount ) {
                        if ( fn.constructor == String )
                                fn = new Function("e", ( !fn.indexOf(".") ? "jQuery(this)" : "return " ) + fn);
+                       if( amount > 0 ) {
+                               var element = this, handler = fn, count = 0;
+                               fn = function(e) {
+                                       if( ++count >= amount )
+                                               jQuery(element).unbind(type, fn);
+                                       handler.apply(element, [e]);
+                               };
+                       }
                        jQuery.event.add( this, type, fn );
                },