Skip to content

Commit 0ed8cc6

Browse files
committed
Added support for map of events in live, die, delegate and undelegate. Fixes #6282.
1 parent 5cba876 commit 0ed8cc6

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/event.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,14 @@ jQuery.each(["live", "die"], function( i, name ) {
940940
var type, i = 0, match, namespaces, preType,
941941
selector = origSelector || this.selector,
942942
context = origSelector ? this : jQuery( this.context );
943+
944+
if ( typeof types === "object" && !types.preventDefault ) {
945+
for ( var key in types ) {
946+
context[ name ]( key, data, types[key], selector );
947+
}
948+
949+
return this;
950+
}
943951

944952
if ( jQuery.isFunction( data ) ) {
945953
fn = data;

test/unit/event.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,45 @@ test("bind/one/unbind(Object)", function(){
206206
equals( mouseoverCounter, 4, "bind(Object)" );
207207
});
208208

209+
test("live/die(Object), delegate/undelegate(String, Object)", function() {
210+
expect(6);
211+
212+
var clickCounter = 0, mouseoverCounter = 0,
213+
$p = jQuery("#firstp"), $a = $p.find("a");
214+
215+
var events = {
216+
click: function( event ) {
217+
clickCounter += ( event.data || 1 );
218+
},
219+
mouseover: function( event ) {
220+
mouseoverCounter += ( event.data || 1 );
221+
}
222+
};
223+
224+
function trigger() {
225+
$a.trigger("click").trigger("mouseover");
226+
}
227+
228+
$a.live( events );
229+
$p.delegate( "a", events, 2 );
230+
231+
trigger();
232+
equals( clickCounter, 3, "live/delegate(Object)" );
233+
equals( mouseoverCounter, 3, "live/delegate(Object)" );
234+
235+
$p.undelegate( "a", events );
236+
237+
trigger();
238+
equals( clickCounter, 4, "undelegate(Object)" );
239+
equals( mouseoverCounter, 4, "undelegate(Object)" );
240+
241+
$a.die( events );
242+
243+
trigger();
244+
equals( clickCounter, 4, "die(Object)" );
245+
equals( mouseoverCounter, 4, "die(Object)" );
246+
});
247+
209248
test("bind(), iframes", function() {
210249
// events don't work with iframes, see #939 - this test fails in IE because of contentDocument
211250
var doc = jQuery("#loadediframe").contents();

0 commit comments

Comments
 (0)