From daca31dc77ac259a1be8a4b0774692ed65e07c22 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Fri, 17 Jan 2014 09:27:10 +0000 Subject: [PATCH] jQuery: added typing for fn --- jquery/jquery-tests.ts | 31 +++++++++++++++++++++++++++++++ jquery/jquery.d.ts | 29 +++++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/jquery/jquery-tests.ts b/jquery/jquery-tests.ts index 95fed2635..1b2fb5d16 100644 --- a/jquery/jquery-tests.ts +++ b/jquery/jquery-tests.ts @@ -2316,6 +2316,27 @@ function test_jQuery() { }); } +function test_fn_extend() { + jQuery.fn.extend({ + check: function () { + return this.each(function () { + this.checked = true; + }); + }, + uncheck: function () { + return this.each(function () { + this.checked = false; + }); + } + }); + + // Use the newly created .check() method + //$( "input[type='checkbox']" ).check(); + // The above test cannot be run as no way that I know of in TypeScript to model the augmentation of jQueryStatic with dynamically added methods + // The below would only work at runtime if extend had first been called. + $("input[type='checkbox']")["check"](); +} + function test_jquery() { var a = { what: "A regular JS object" }, b = $('body'); @@ -2422,6 +2443,16 @@ function test_jquery() { jQuery(function ($) { // Your code using failsafe $ alias here... }); + + $(document.body) + .click(function () { + $(document.body).append($("
")); + var n = $("div").length; + $("span").text("There are " + n + " divs." + + "Click to add more."); + }) + // Trigger the click to start + .trigger("click"); } function test_keydown() { diff --git a/jquery/jquery.d.ts b/jquery/jquery.d.ts index 9fc3b1dff..9b412c5b5 100644 --- a/jquery/jquery.d.ts +++ b/jquery/jquery.d.ts @@ -778,12 +778,33 @@ interface JQueryStatic { Event: JQueryEventConstructor; - // Internals - error(message: any): JQuery; + /** + * Takes a string and throws an exception containing it. + * + * @param message The message to send out. + */ + error(message: string): JQuery; - // Miscellaneous expr: any; - fn: any; //TODO: Decide how we want to type this + fn: { + /** + * A string containing the jQuery version number. + */ + jquery: string; + + /** + * The number of elements in the jQuery object. + */ + length: number; + + /** + * Merge the contents of an object onto the jQuery prototype to provide new jQuery instance methods. + * + * @param object An object to merge onto the jQuery prototype. + */ + extend(object: any): any; + } + isReady: boolean; // Properties