From fa27dfe8576bf9505a22b35476914e9c09e6545c Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Fri, 7 Dec 2018 13:23:48 -0500 Subject: [PATCH] [jquery] Fix issue with unbinding event handlers for specific event types. --- types/jquery/JQuery.d.ts | 32 +++++++++++++++++++------------- types/jquery/jquery-tests.ts | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/types/jquery/JQuery.d.ts b/types/jquery/JQuery.d.ts index 123fad3b34..e9fd87263f 100644 --- a/types/jquery/JQuery.d.ts +++ b/types/jquery/JQuery.d.ts @@ -7304,10 +7304,12 @@ $( "body" ).on( "click", "p", foo ); $( "body" ).off( "click", "p", foo ); ``` */ - off(events: string, + off( + events: TType, selector: JQuery.Selector, - handler: JQuery.TypeEventHandler | - false): this; + handler: JQuery.TypeEventHandler | + false + ): this; /** * Remove an event handler. * @param events One or more space-separated event types and optional namespaces, or just namespaces, such as @@ -7337,10 +7339,12 @@ $( "form" ).on( "keypress.validator", "input[type='text']", validate ); $( "form" ).off( ".validator" ); ``` */ - off(events: string, + off( + events: TType, selector_handler?: JQuery.Selector | - JQuery.TypeEventHandler | - false): this; + JQuery.TypeEventHandler | + false + ): this; /** * Remove an event handler. * @param events An object where the string keys represent one or more space-separated event types and optional @@ -11968,9 +11972,10 @@ $( "p" ).bind( "click", foo ); // ... Now foo will be called when paragraphs are $( "p" ).unbind( "click", foo ); // ... foo will no longer be called. ``` */ - unbind(event: string, - handler: JQuery.TypeEventHandler | - false + unbind( + event: TType, + handler: JQuery.TypeEventHandler | + false ): this; /** * Remove a previously-attached event handler from the elements. @@ -12062,10 +12067,11 @@ $( "body" ).delegate( "p", "click", foo ); $( "body" ).undelegate( "p", "click", foo ); ``` */ - undelegate(selector: JQuery.Selector, - eventType: string, - handler: JQuery.TypeEventHandler | - false + undelegate( + selector: JQuery.Selector, + eventType: TType, + handler: JQuery.TypeEventHandler | + false ): this; /** * Remove a handler from the event for all elements which match the current selector, based upon a specific set of root elements. diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index c533f1b235..1ede9fc8f5 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -3969,12 +3969,17 @@ function JQuery() { function customData(this: HTMLElement, event: JQuery.TriggeredEvent) { } + function specificEventType(this: HTMLElement, event: JQuery.MouseDownEvent) { } + // $ExpectType JQuery $('table').off('myEvent', 'td', defaultData); // $ExpectType JQuery $('table').off('myEvent', 'td', customData); + // $ExpectType JQuery + $('table').off('mousedown', 'td', specificEventType); + // $ExpectType JQuery $('table').off('myEvent', 'td', false); @@ -3987,6 +3992,9 @@ function JQuery() { // $ExpectType JQuery $('table').off('myEvent', customData); + // $ExpectType JQuery + $('table').off('mousedown', specificEventType); + // $ExpectType JQuery $('table').off('myEvent', false); @@ -4473,12 +4481,17 @@ function JQuery() { function customData(this: HTMLElement, event: JQuery.TriggeredEvent) { } + function specificEventType(this: HTMLElement, event: JQuery.MouseDownEvent) { } + // $ExpectType JQuery $('p').unbind('myEvent', defaultData); // $ExpectType JQuery $('p').unbind('myEvent', customData); + // $ExpectType JQuery + $('p').unbind('mousedown', specificEventType); + // $ExpectType JQuery $('p').unbind('myEvent', false); @@ -4498,12 +4511,17 @@ function JQuery() { function customData(this: HTMLElement, event: JQuery.TriggeredEvent) { } + function specificEventType(this: HTMLElement, event: JQuery.MouseDownEvent) { } + // $ExpectType JQuery $('table').undelegate('td', 'click', defaultData); // $ExpectType JQuery $('table').undelegate('td', 'click', customData); + // $ExpectType JQuery + $('table').undelegate('td', 'mousedown', specificEventType); + // $ExpectType JQuery $('table').undelegate('td', 'click', false);