Skip to content

Commit 9584e90

Browse files
committed
Added in Ben Alman's proposed event.namespace property (the property holds the namespaces specified in a call to trigger). Additionally fixes namespaces with .live(). Fixes #6208 and #6209.
1 parent 04e31ff commit 9584e90

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

src/event.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ jQuery.event = {
368368
},
369369

370370
handle: function( event ) {
371-
var all, handlers, namespaces, namespace, events, args = jQuery.makeArray( arguments );
371+
var all, handlers, namespaces, namespace_sort = [], namespace_re, events, args = jQuery.makeArray( arguments );
372372

373373
event = args[0] = jQuery.event.fix( event || window.event );
374374
event.currentTarget = this;
@@ -379,7 +379,9 @@ jQuery.event = {
379379
if ( !all ) {
380380
namespaces = event.type.split(".");
381381
event.type = namespaces.shift();
382-
namespace = new RegExp("(^|\\.)" + namespaces.slice(0).sort().join("\\.(?:.*\\.)?") + "(\\.|$)");
382+
namespace_sort = namespaces.slice(0).sort();
383+
namespace_re = new RegExp("(^|\\.)" + namespace_sort.join("\\.(?:.*\\.)?") + "(\\.|$)");
384+
event.namespace = namespace_sort.join(".");
383385
}
384386

385387
events = jQuery.data(this, "events");
@@ -393,7 +395,7 @@ jQuery.event = {
393395
var handleObj = handlers[ j ];
394396

395397
// Filter the functions by class
396-
if ( all || namespace.test( handleObj.namespace ) ) {
398+
if ( all || namespace_re.test( handleObj.namespace ) ) {
397399
// Pass in a reference to the handler function itself
398400
// So that we can later remove it
399401
event.handler = handleObj.handler;
@@ -997,14 +999,18 @@ jQuery.each(["live", "die"], function( i, name ) {
997999

9981000
function liveHandler( event ) {
9991001
var stop, maxLevel, elems = [], selectors = [],
1000-
related, match, handleObj, elem, j, i, l, data, close,
1002+
related, match, handleObj, elem, j, i, l, data, close, namespace,
10011003
events = jQuery.data( this, "events" );
10021004

10031005
// Make sure we avoid non-left-click bubbling in Firefox (#3861)
10041006
if ( event.liveFired === this || !events || !events.live || event.button && event.type === "click" ) {
10051007
return;
10061008
}
10071009

1010+
if ( event.namespace ) {
1011+
namespace = new RegExp("(^|\\.)" + event.namespace.split(".").join("\\.(?:.*\\.)?") + "(\\.|$)");
1012+
}
1013+
10081014
event.liveFired = this;
10091015

10101016
var live = events.live.slice(0);
@@ -1028,7 +1034,7 @@ function liveHandler( event ) {
10281034
for ( j = 0; j < live.length; j++ ) {
10291035
handleObj = live[j];
10301036

1031-
if ( close.selector === handleObj.selector ) {
1037+
if ( close.selector === handleObj.selector && (!namespace || namespace.test( handleObj.namespace )) ) {
10321038
elem = close.elem;
10331039
related = null;
10341040

test/unit/event.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,41 +1135,55 @@ test("live with multiple events", function(){
11351135
});
11361136

11371137
test("live with namespaces", function(){
1138-
expect(6);
1138+
expect(12);
11391139

11401140
var count1 = 0, count2 = 0;
11411141

1142-
jQuery("#liveSpan1").live("foo.bar", function(){
1142+
jQuery("#liveSpan1").live("foo.bar", function(e){
11431143
count1++;
11441144
});
11451145

1146-
jQuery("#liveSpan2").live("foo.zed", function(){
1146+
jQuery("#liveSpan1").live("foo.zed", function(e){
11471147
count2++;
11481148
});
11491149

11501150
jQuery("#liveSpan1").trigger("foo.bar");
11511151
equals( count1, 1, "Got live foo.bar" );
1152+
equals( count2, 0, "Got live foo.bar" );
1153+
1154+
count1 = 0, count2 = 0;
11521155

1153-
jQuery("#liveSpan2").trigger("foo.zed");
1156+
jQuery("#liveSpan1").trigger("foo.zed");
1157+
equals( count1, 0, "Got live foo.zed" );
11541158
equals( count2, 1, "Got live foo.zed" );
11551159

11561160
//remove one
1157-
jQuery("#liveSpan2").die("foo.zed");
1161+
count1 = 0, count2 = 0;
1162+
1163+
jQuery("#liveSpan1").die("foo.zed");
11581164
jQuery("#liveSpan1").trigger("foo.bar");
11591165

1160-
equals( count1, 2, "Got live foo.bar after dieing foo.zed" );
1166+
equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1167+
equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
11611168

1162-
jQuery("#liveSpan2").trigger("foo.zed");
1163-
equals( count2, 1, "Got live foo.zed" );
1169+
count1 = 0, count2 = 0;
1170+
1171+
jQuery("#liveSpan1").trigger("foo.zed");
1172+
equals( count1, 0, "Got live foo.zed" );
1173+
equals( count2, 0, "Got live foo.zed" );
11641174

11651175
//remove the other
11661176
jQuery("#liveSpan1").die("foo.bar");
11671177

1178+
count1 = 0, count2 = 0;
1179+
11681180
jQuery("#liveSpan1").trigger("foo.bar");
1169-
equals( count1, 2, "Did not respond to foo.bar after dieing it" );
1181+
equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1182+
equals( count2, 0, "Did not respond to foo.bar after dieing it" );
11701183

1171-
jQuery("#liveSpan2").trigger("foo.zed");
1172-
equals( count2, 1, "Did not trigger foo.zed again" );
1184+
jQuery("#liveSpan1").trigger("foo.zed");
1185+
equals( count1, 0, "Did not trigger foo.zed again" );
1186+
equals( count2, 0, "Did not trigger foo.zed again" );
11731187
});
11741188

11751189
test("live with change", function(){

0 commit comments

Comments
 (0)