From 122ff1fb4c1551da6fd522cdaa42b9548ae29913 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Fri, 14 Feb 2014 16:36:27 +0000 Subject: [PATCH 1/2] jQuery: trigger now JSDoc'd and typings brought in line with documentation. Also added test suite. --- jquery/jquery-tests.ts | 55 ++++++++++++++++++++++++++++++++++++++++++ jquery/jquery.d.ts | 30 +++++++++++++++++++++-- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/jquery/jquery-tests.ts b/jquery/jquery-tests.ts index c3fc3032f3..a6249b9f37 100644 --- a/jquery/jquery-tests.ts +++ b/jquery/jquery-tests.ts @@ -754,6 +754,61 @@ function test_submit() { $("#target").submit(); } +function test_trigger() { + + $("#foo").on("click", function () { + alert($(this).text()); + }); + $("#foo").trigger("click"); + + //$("#foo").on("custom", function (event, param1, param2) { + // alert(param1 + "\n" + param2); + //}); + $("#foo").trigger("custom", ["Custom", "Event"]); + + $("button:first").click(function () { + update($("span:first")); + }); + + $("button:last").click(function () { + $("button:first").trigger("click"); + update($("span:last")); + }); + + function update(j) { + var n = parseInt(j.text(), 10); + j.text(n + 1); + } + + $("form:first").trigger("submit"); + + var event = jQuery.Event("submit"); + $("form:first").trigger(event); + if (event.isDefaultPrevented()) { + // Perform an action... + } + + $("p") + .click(function (event, a, b) { + // When a normal click fires, a and b are undefined + // for a trigger like below a refers to "foo" and b refers to "bar" + }) + .trigger("click", ["foo", "bar"]); + + var event = jQuery.Event("logged"); + (event).user = "foo"; + (event).pass = "bar"; + $("body").trigger(event); + + // Adapted from jQuery documentation which may be wrong on this occasion + var event2 = jQuery.Event("logged"); + $("body").trigger(event2, { + type: "logged", + user: "foo", + pass: "bar" + }); +} + function test_clone() { $('.hello').clone().appendTo('.goodbye'); var $elem = $('#elem').data({ "arr": [1] }), diff --git a/jquery/jquery.d.ts b/jquery/jquery.d.ts index bc7886e448..235022bf99 100644 --- a/jquery/jquery.d.ts +++ b/jquery/jquery.d.ts @@ -2531,8 +2531,34 @@ interface JQuery { */ submit(eventData?: any, handler?: (eventObject: JQueryEventObject) => any): JQuery; - trigger(eventType: string, ...extraParameters: any[]): JQuery; - trigger(event: JQueryEventObject): JQuery; + /** + * Execute all handlers and behaviors attached to the matched elements for the given event type. + * + * @param eventType A string containing a JavaScript event type, such as click or submit. + * @param extraParameters Additional parameters to pass along to the event handler. + */ + trigger(eventType: string, extraParameters?: any[]): JQuery; + /** + * Execute all handlers and behaviors attached to the matched elements for the given event type. + * + * @param eventType A string containing a JavaScript event type, such as click or submit. + * @param extraParameters Additional parameters to pass along to the event handler. + */ + trigger(eventType: string, extraParameters?: Object): JQuery; + /** + * Execute all handlers and behaviors attached to the matched elements for the given event type. + * + * @param event A jQuery.Event object. + * @param extraParameters Additional parameters to pass along to the event handler. + */ + trigger(event: JQueryEventObject, extraParameters?: any[]): JQuery; + /** + * Execute all handlers and behaviors attached to the matched elements for the given event type. + * + * @param event A jQuery.Event object. + * @param extraParameters Additional parameters to pass along to the event handler. + */ + trigger(event: JQueryEventObject, extraParameters?: Object): JQuery; triggerHandler(eventType: string, ...extraParameters: any[]): Object; From 4c2d50af485fbe47178284ed7291fa4c57110e3c Mon Sep 17 00:00:00 2001 From: John Reilly Date: Fri, 14 Feb 2014 16:48:11 +0000 Subject: [PATCH 2/2] jQuery: uncommented tests realised they could be used as long as param1 and param2 made optional --- jquery/jquery-tests.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jquery/jquery-tests.ts b/jquery/jquery-tests.ts index a6249b9f37..57ba838ee3 100644 --- a/jquery/jquery-tests.ts +++ b/jquery/jquery-tests.ts @@ -761,9 +761,9 @@ function test_trigger() { }); $("#foo").trigger("click"); - //$("#foo").on("custom", function (event, param1, param2) { - // alert(param1 + "\n" + param2); - //}); + $("#foo").on("custom", function (event, param1?, param2?) { + alert(param1 + "\n" + param2); + }); $("#foo").trigger("custom", ["Custom", "Event"]); $("button:first").click(function () {