From a6d7470f12261f0359e31aa2bcb53a3542273d7f Mon Sep 17 00:00:00 2001 From: Junle Li Date: Tue, 10 Jun 2014 23:30:45 +0800 Subject: [PATCH 1/5] Add jQuery.ajax test to verify a bug. The definition does not leverage the returned value of $.ajax() method well. The official syntax is ``` jqXHR.then(function( data, textStatus, jqXHR ) {}, function( jqXHR, textStatus, errorThrown ) {}); ``` (From: http://api.jquery.com/jQuery.ajax/) --- jquery/jquery-tests.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/jquery/jquery-tests.ts b/jquery/jquery-tests.ts index 79dda3e54..30cb2111a 100644 --- a/jquery/jquery-tests.ts +++ b/jquery/jquery-tests.ts @@ -150,6 +150,15 @@ function test_ajax() { url: "test.js", dataType: "script" }); + + // treat $.ajax() as a promise (as of 1.8) + $.ajax({ + url: "test.js" + }).then((data, textStatus, jqXHR) => { + console.log(data, textStatus, jqXHR); + }, (jqXHR, textStatus, errorThrown) => { + console.log(jqXHR, textStatus, errorThrown); + }); } function test_ajaxComplete() { From 459de609146d3978c10dcbe5d45ddae3424792b8 Mon Sep 17 00:00:00 2001 From: Junle Li Date: Tue, 10 Jun 2014 23:33:48 +0800 Subject: [PATCH 2/5] Remove tailing space from jquery-test file --- jquery/jquery-tests.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/jquery/jquery-tests.ts b/jquery/jquery-tests.ts index 30cb2111a..63c4cdeb5 100644 --- a/jquery/jquery-tests.ts +++ b/jquery/jquery-tests.ts @@ -736,7 +736,7 @@ function test_callbacksFunctions() { callbacks.add(bar); callbacks.fire('world'); callbacks.disable(); - + // Test the disabled state of the list console.log(callbacks.disabled()); // Outputs: true @@ -3301,4 +3301,3 @@ function test_deferred_promise() { } ); } - From fc628f15ad1f9a4681ff1c80bcb8f07574f0fa76 Mon Sep 17 00:00:00 2001 From: Junle Li Date: Wed, 11 Jun 2014 00:50:36 +0800 Subject: [PATCH 3/5] Write defition for JQueryXHR.then method. Working on borisyankov/DefinitelyTyped#2314 --- jquery/jquery.d.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/jquery/jquery.d.ts b/jquery/jquery.d.ts index d6a6a792e..62b1a3fea 100644 --- a/jquery/jquery.d.ts +++ b/jquery/jquery.d.ts @@ -168,6 +168,10 @@ interface JQueryXHR extends XMLHttpRequest, JQueryPromise { */ overrideMimeType(mimeType: string): any; abort(statusText?: string): void; + /** + * Incorporates the functionality of the .done() and .fail() methods, allowing (as of jQuery 1.8) the underlying Promise to be manipulated. Refer to deferred.then() for implementation details. + */ + then(doneCallback: (data: any, textStatus: string, jqXHR: JQueryXHR) => void, failCallback: (jqXHR: JQueryXHR, textStatus: string, errorThrown: any) => void): JQueryPromise; } /** From 52678f3b877e2e619b589afcd0d6f9fcab9e2f08 Mon Sep 17 00:00:00 2001 From: Junle Li Date: Wed, 11 Jun 2014 01:13:40 +0800 Subject: [PATCH 4/5] Add specs for jqXHR object returned by $.ajax The tests are corresponding to the syntax listed in its website: http://api.jquery.com/jQuery.ajax/#jqXHR --- jquery/jquery-tests.ts | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/jquery/jquery-tests.ts b/jquery/jquery-tests.ts index 63c4cdeb5..61b15b20b 100644 --- a/jquery/jquery-tests.ts +++ b/jquery/jquery-tests.ts @@ -151,7 +151,38 @@ function test_ajax() { dataType: "script" }); - // treat $.ajax() as a promise (as of 1.8) + // Test the jqXHR object returned by $.ajax() as of 1.5 + // More details: http://api.jquery.com/jQuery.ajax/#jqXHR + + // done method + $.ajax({ + url: "test.js" + }).done((data, textStatus, jqXHR) => { + console.log(data, textStatus, jqXHR); + }); + + // fail method + $.ajax({ + url: "test.js" + }).fail((jqXHR, textStatus, errorThrown) => { + console.log(jqXHR, textStatus, errorThrown); + }); + + // always method with successful request + $.ajax({ + url: "test.js" + }).always((data, textStatus, jqXHR) => { + console.log(data, textStatus, jqXHR); + }); + + // always method with failed request + $.ajax({ + url: "test.js" + }).always((jqXHR, textStatus, errorThrown) => { + console.log(jqXHR, textStatus, errorThrown); + }); + + // then method (as of 1.8) $.ajax({ url: "test.js" }).then((data, textStatus, jqXHR) => { From cd8356008b4b160b093440dbbd417d19b9d76808 Mon Sep 17 00:00:00 2001 From: Junle Li Date: Mon, 16 Jun 2014 01:42:56 +0800 Subject: [PATCH 5/5] Fix a asterisk bug. --- jquery/jquery.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery/jquery.d.ts b/jquery/jquery.d.ts index 213d500b2..b026c0e5d 100644 --- a/jquery/jquery.d.ts +++ b/jquery/jquery.d.ts @@ -172,7 +172,7 @@ interface JQueryXHR extends XMLHttpRequest, JQueryPromise { * Incorporates the functionality of the .done() and .fail() methods, allowing (as of jQuery 1.8) the underlying Promise to be manipulated. Refer to deferred.then() for implementation details. */ then(doneCallback: (data: any, textStatus: string, jqXHR: JQueryXHR) => void, failCallback: (jqXHR: JQueryXHR, textStatus: string, errorThrown: any) => void): JQueryPromise; - /* + /** * Property containing the parsed response if the response Content-Type is json */ responseJSON?: any;