Allow mapped types to be removed by a namespace-only type name.

This commit is contained in:
Dave Methvin 2011-11-15 11:38:55 -05:00
parent 6736dd7168
commit ca8fc725ac
2 changed files with 15 additions and 7 deletions

View File

@ -148,7 +148,7 @@ jQuery.event = {
global: {},
// Detach an event or set of events from an element
remove: function( elem, types, handler, selector ) {
remove: function( elem, types, handler, selector, mappedTypes ) {
var elemData = jQuery.hasData( elem ) && jQuery._data( elem ),
t, tns, type, origType, namespaces, origCount,
@ -167,9 +167,8 @@ jQuery.event = {
// Unbind all events (on this namespace, if provided) for the element
if ( !type ) {
namespaces = namespaces? "." + namespaces : "";
for ( j in events ) {
jQuery.event.remove( elem, j + namespaces, handler, selector );
for ( type in events ) {
jQuery.event.remove( elem, type + types[ t ], handler, selector, true );
}
continue;
}
@ -184,7 +183,7 @@ jQuery.event = {
for ( j = 0; j < eventType.length; j++ ) {
handleObj = eventType[ j ];
if ( origType === handleObj.origType &&
if ( ( mappedTypes || origType === handleObj.origType ) &&
( !handler || handler.guid === handleObj.guid ) &&
( !namespaces || namespaces.test( handleObj.namespace ) ) &&
( !selector || selector === handleObj.selector || selector === "**" && handleObj.selector ) ) {

View File

@ -1132,7 +1132,7 @@ test("trigger(eventObject, [data], [fn])", function() {
$child.unbind();
$parent.unbind().remove();
// Ensure triggerHandler doesn't molest its event object (#xxx)
var event = jQuery.Event( "zowie" );
jQuery( document ).triggerHandler( event );
@ -2508,7 +2508,6 @@ test("special bind/delegate name mapping", function() {
.remove();
delete jQuery.event.special.slap;
// Ensure a special event isn't removed by its mapped type
jQuery.event.special.gutfeeling = {
bindType: "click",
delegateType: "click",
@ -2516,12 +2515,22 @@ test("special bind/delegate name mapping", function() {
equal( event.handleObj.origType, "gutfeeling", "got a gutfeeling" );
}
};
// Ensure a special event isn't removed by its mapped type
jQuery( '<p>Gut Feeling</p>' )
.on( "click", jQuery.noop )
.on( "gutfeeling", jQuery.noop )
.off( "click" )
.trigger( "gutfeeling" )
.remove();
// Ensure special events are removed when only a namespace is provided
jQuery( '<p>Gut Feeling</p>' )
.on( "gutfeeling.Devo", jQuery.noop )
.off( ".Devo" )
.trigger( "gutfeeling" )
.remove();
delete jQuery.event.special.gutfeeling;
});