🌐 AI搜索 & 代理 主页
Skip to content

Commit 71efbdd

Browse files
committed
refactor specialAll into add and remove hooks for existing special events, live now accepts optional data param like bind, fixes #4612 and #4613, thanks to Mike Helgeson
1 parent 287ecdb commit 71efbdd

File tree

2 files changed

+38
-42
lines changed

2 files changed

+38
-42
lines changed

src/event.js

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,15 @@ jQuery.event = {
6060
handler.type = namespaces.slice().sort().join(".");
6161

6262
// Get the current list of functions bound to this event
63-
var handlers = events[ type ];
64-
65-
if ( this.specialAll[ type ] ) {
66-
this.specialAll[ type ].setup.call( elem, data, namespaces );
63+
var handlers = events[ type ],
64+
special = this.special[ type ] || {};
65+
66+
if ( special.add ) {
67+
var modifiedHandler = special.add.call( elem, handler, data, namespaces );
68+
if ( modifiedHandler && jQuery.isFunction( modifiedHandler ) ) {
69+
modifiedHandler.guid = handler.guid;
70+
handler = modifiedHandler;
71+
}
6772
}
6873

6974
// Init the event handler queue
@@ -73,7 +78,7 @@ jQuery.event = {
7378
// Check for a special event handler
7479
// Only use addEventListener/attachEvent if the special
7580
// events handler returns false
76-
if ( !this.special[ type ] || this.special[ type ].setup.call( elem, data, namespaces ) === false ) {
81+
if ( !special.setup || special.setup.call( elem, data, namespaces ) === false ) {
7782
// Bind the global event handler to the element
7883
if ( elem.addEventListener ) {
7984
elem.addEventListener( type, handle, false );
@@ -128,9 +133,10 @@ jQuery.event = {
128133
var namespaces = type.split(".");
129134
type = namespaces.shift();
130135
var all = !namespaces.length,
131-
namespace = new RegExp("(^|\\.)" + namespaces.slice().sort().join(".*\\.") + "(\\.|$)");
136+
namespace = new RegExp("(^|\\.)" + namespaces.slice().sort().join(".*\\.") + "(\\.|$)"),
137+
special = this.special[ type ] || {};
132138

133-
if ( events[type] ) {
139+
if ( events[ type ] ) {
134140
// remove the given handler for the given type
135141
if ( handler ) {
136142
delete events[ type ][ handler.guid ];
@@ -145,8 +151,8 @@ jQuery.event = {
145151
}
146152
}
147153

148-
if ( this.specialAll[ type ] ) {
149-
this.specialAll[ type ].teardown.call( elem, namespaces );
154+
if ( special.remove ) {
155+
special.remove.call( elem, namespaces );
150156
}
151157

152158
// remove generic event handler if no more handlers exist
@@ -381,28 +387,17 @@ jQuery.event = {
381387
// Make sure the ready event is setup
382388
setup: bindReady,
383389
teardown: function() {}
384-
}
385-
},
386-
387-
specialAll: {
390+
},
391+
388392
live: {
389-
setup: function( selector, namespaces ) {
390-
jQuery.event.add( this, namespaces[0], liveHandler );
393+
add: function( proxy, data, namespaces ) {
394+
jQuery.extend( proxy, data || {} );
395+
proxy.guid += data.selector + data.live;
396+
jQuery.event.add( this, data.live, liveHandler );
391397
},
392-
teardown: function( namespaces ) {
393-
if ( namespaces.length ) {
394-
var remove = 0, name = new RegExp("(^|\\.)" + namespaces[0] + "(\\.|$)");
395-
396-
jQuery.each( (jQuery.data(this, "events").live || {}), function() {
397-
if ( name.test(this.type) ) {
398-
remove++;
399-
}
400-
});
401-
402-
if ( remove < 1 ) {
403-
jQuery.event.remove( this, namespaces[0], liveHandler );
404-
}
405-
}
398+
399+
teardown: function( namespaces ) {
400+
jQuery.event.remove( this, namespaces[0], liveHandler );
406401
}
407402
}
408403
}
@@ -592,28 +587,25 @@ jQuery.fn.extend({
592587
return this;
593588
},
594589

595-
live: function( type, fn ) {
596-
var proxy = jQuery.event.proxy( fn );
597-
proxy.guid += this.selector + type;
598-
599-
jQuery( this.context ).bind( liveConvert( type, this.selector ), this.selector, proxy );
600-
590+
live: function( type, data, fn ) {
591+
jQuery( this.context ).bind( liveConvert( type, this.selector ), {
592+
data: fn && data, selector: this.selector, live: type
593+
}, fn || data );
601594
return this;
602595
},
603596

604597
die: function( type, fn ) {
605-
jQuery( this.context ).unbind( liveConvert(type, this.selector), fn ? { guid: fn.guid + this.selector + type } : null );
598+
jQuery( this.context ).unbind( liveConvert( type, this.selector ), fn ? { guid: fn.guid + this.selector + type } : null );
606599
return this;
607600
}
608601
});
609602

610603
function liveHandler( event ) {
611-
var check = new RegExp("(^|\\.)" + event.type + "(\\.|$)"),
612-
stop = true, elems = [];
604+
var stop = true, elems = [];
613605

614606
jQuery.each( jQuery.data( this, "events" ).live || [], function( i, fn ) {
615-
if ( check.test( fn.type ) ) {
616-
var elem = jQuery( event.target ).closest( fn.data )[0];
607+
if ( fn.live === event.type ) {
608+
var elem = jQuery( event.target ).closest( fn.selector )[0];
617609
if ( elem ) {
618610
elems.push({ elem: elem, fn: fn });
619611
}
@@ -626,7 +618,8 @@ function liveHandler( event ) {
626618

627619
jQuery.each(elems, function() {
628620
event.currentTarget = this.elem;
629-
if ( this.fn.call( this.elem, event, this.fn.data ) === false ) {
621+
event.data = this.fn.data
622+
if ( this.fn.call( this.elem, event, this.fn.selector ) === false ) {
630623
return (stop = false);
631624
}
632625
});

test/unit/event.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ test("toggle(Function, Function, ...)", function() {
490490
});
491491

492492
test(".live()/.die()", function() {
493-
expect(52);
493+
expect(53);
494494

495495
var submit = 0, div = 0, livea = 0, liveb = 0;
496496

@@ -579,6 +579,9 @@ test(".live()/.die()", function() {
579579
jQuery("#foo").trigger('click');
580580
equals( clicked, 2, "die with a context");
581581

582+
// Test binding with event data
583+
jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
584+
jQuery("#foo").trigger("click").die("click");
582585

583586
// Verify that return false prevents default action
584587
jQuery("#anchor2").live("click", function(){ return false; });

0 commit comments

Comments
 (0)