From 50bc863d8c142aefda434259ca60b6781bb7b087 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Mon, 24 Feb 2014 14:49:28 +0000 Subject: [PATCH 1/2] jQuery: JSDoc'd unbind and added test suite --- jquery/jquery-tests.ts | 64 ++++++++++++++++++++++++++++++++++++++++++ jquery/jquery.d.ts | 23 +++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/jquery/jquery-tests.ts b/jquery/jquery-tests.ts index 7177b13330..37cf5495ec 100644 --- a/jquery/jquery-tests.ts +++ b/jquery/jquery-tests.ts @@ -590,6 +590,70 @@ function test_bind() { }); } +function test_unbind() { + $("#foo").unbind(); + + $("#foo").unbind("click"); + + var handler = function () { + alert("The quick brown fox jumps over the lazy dog."); + }; + $("#foo").bind("click", handler); + $("#foo").unbind("click", handler); + + $("#foo").bind("click", function () { + alert("The quick brown fox jumps over the lazy dog."); + }); + + // Will NOT work + $("#foo").unbind("click", function () { + alert("The quick brown fox jumps over the lazy dog."); + }); + + $("#foo").bind("click.myEvents", handler); + + $("#foo").unbind("click"); + + $("#foo").unbind("click.myEvents"); + + $("#foo").unbind(".myEvents"); + + var timesClicked = 0; + $("#foo").bind("click", function (event) { + alert("The quick brown fox jumps over the lazy dog."); + timesClicked++; + if (timesClicked >= 3) { + $(this).unbind(event); + } + }); + + function aClick() { + $("div").show().fadeOut("slow"); + } + $("#bind").click(function () { + $("#theone") + .bind("click", aClick) + .text("Can Click!"); + }); + $("#unbind").click(function () { + $("#theone") + .unbind("click", aClick) + .text("Does nothing..."); + }); + + $("p").unbind(); + + $("p").unbind("click"); + + var foo = function () { + // Code to handle some kind of event + }; + + $("p").bind("click", foo); // ... Now foo will be called when paragraphs are clicked ... + + $("p").unbind("click", foo); // ... foo will no longer be called. +} + function test_blur() { $('#target').blur(function () { alert('Handler for .blur() called.'); diff --git a/jquery/jquery.d.ts b/jquery/jquery.d.ts index 83f2288d0c..76e5172f33 100644 --- a/jquery/jquery.d.ts +++ b/jquery/jquery.d.ts @@ -2605,10 +2605,33 @@ interface JQuery { */ trigger(event: JQueryEventObject, extraParameters?: Object): JQuery; + /** + * Execute all handlers attached to an element for an event. + * + * @param eventType A string containing a JavaScript event type, such as click or submit. + * @param extraParameters An array of additional parameters to pass along to the event handler. + */ triggerHandler(eventType: string, ...extraParameters: any[]): Object; + /** + * Remove a previously-attached event handler from the elements. + * + * @param eventType A string containing a JavaScript event type, such as click or submit. + * @param handler The function that is to be no longer executed. + */ unbind(eventType?: string, handler?: (eventObject: JQueryEventObject) => any): JQuery; + /** + * Remove a previously-attached event handler from the elements. + * + * @param eventType A string containing a JavaScript event type, such as click or submit. + * @param fls Unbinds the corresponding 'return false' function that was bound using .bind( eventType, false ). + */ unbind(eventType: string, fls: boolean): JQuery; + /** + * Remove a previously-attached event handler from the elements. + * + * @param evt A JavaScript event object as passed to an event handler. + */ unbind(evt: any): JQuery; undelegate(): JQuery; From 69f36f7e0bbf6bd29415b5a609588c5f7d5286c4 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Mon, 24 Feb 2014 14:57:10 +0000 Subject: [PATCH 2/2] jQuery: JSDoc'd undelegate added delegate / undelegate test suites --- jquery/jquery-tests.ts | 46 +++++++++++++++++++++++++++++++++++++++--- jquery/jquery.d.ts | 25 +++++++++++++++++++++-- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/jquery/jquery-tests.ts b/jquery/jquery-tests.ts index 37cf5495ec..cfd4ae56f4 100644 --- a/jquery/jquery-tests.ts +++ b/jquery/jquery-tests.ts @@ -1183,7 +1183,6 @@ function test_delay() { }); } -/* Not existing, but not recommended either function test_delegate() { $("table").delegate("td", "click", function () { $(this).toggleClass("chosen"); @@ -1201,7 +1200,7 @@ function test_delegate() { $("body").delegate("a", "click", function (event) { event.preventDefault(); }); - $("body").delegate("p", "myCustomEvent", function (e, myName, myValue) { + $("body").delegate("p", "myCustomEvent", function (e, myName?, myValue?) { $(this).text("Hi there!"); $("span").stop().css("opacity", 1) .text("myName = " + myName) @@ -1211,7 +1210,48 @@ function test_delegate() { $("p").trigger("myCustomEvent"); }); } -*/ + +function test_undelegate() { + function aClick() { + $("div").show().fadeOut("slow"); + } + $("#bind").click(function () { + $("body") + .delegate("#theone", "click", aClick) + .find("#theone").text("Can Click!"); + }); + $("#unbind").click(function () { + $("body") + .undelegate("#theone", "click", aClick) + .find("#theone").text("Does nothing..."); + }); + + $("p").undelegate(); + + $("p").undelegate("click"); + + var foo = function () { + // Code to handle some kind of event + }; + + // ... Now foo will be called when paragraphs are clicked ... + $("body").delegate("p", "click", foo); + + // ... foo will no longer be called. + $("body").undelegate("p", "click", foo); + + var foo = function () { + // Code to handle some kind of event + }; + + // Delegate events under the ".whatever" namespace + $("form").delegate(":button", "click.whatever", foo); + + $("form").delegate("input[type='text'] ", "keypress.whatever", foo); + + // Unbind all events delegated under the ".whatever" namespace + $("form").undelegate(".whatever"); +} function test_dequeue() { $("button").click(function () { diff --git a/jquery/jquery.d.ts b/jquery/jquery.d.ts index 76e5172f33..329e0a177e 100644 --- a/jquery/jquery.d.ts +++ b/jquery/jquery.d.ts @@ -2634,9 +2634,30 @@ interface JQuery { */ unbind(evt: any): JQuery; + /** + * Remove a handler from the event for all elements which match the current selector, based upon a specific set of root elements. + */ undelegate(): JQuery; - undelegate(selector: any, eventType: string, handler?: (eventObject: JQueryEventObject) => any): JQuery; - undelegate(selector: any, events: any): JQuery; + /** + * Remove a handler from the event for all elements which match the current selector, based upon a specific set of root elements. + * + * @param selector A selector which will be used to filter the event results. + * @param eventType A string containing a JavaScript event type, such as "click" or "keydown" + * @param handler A function to execute at the time the event is triggered. + */ + undelegate(selector: string, eventType: string, handler?: (eventObject: JQueryEventObject) => any): JQuery; + /** + * Remove a handler from the event for all elements which match the current selector, based upon a specific set of root elements. + * + * @param selector A selector which will be used to filter the event results. + * @param events An object of one or more event types and previously bound functions to unbind from them. + */ + undelegate(selector: string, events: Object): JQuery; + /** + * Remove a handler from the event for all elements which match the current selector, based upon a specific set of root elements. + * + * @param namespace A string containing a namespace to unbind all events from. + */ undelegate(namespace: string): JQuery; unload(handler: (eventObject: JQueryEventObject) => any): JQuery;