Permalink
Browse files

Added $.ajax timeout setting

  • Loading branch information...
1 parent 83f2a4f commit f95c8e5cacfe194ef201aa4c5974e6011d3c175b @trabianmatt trabianmatt committed Jan 29, 2013
Showing with 50 additions and 4 deletions.
  1. +3 −2 src/tb/lib/ti/ajax/index.coffee
  2. +26 −0 test/tb/lib/ti/ajax/settings_test.coffee
  3. +8 −1 ti/dist.js
  4. +13 −1 ti/src/network/index.coffee
View
5 src/tb/lib/ti/ajax/index.coffee
@@ -307,6 +307,8 @@ module.exports = ($) ->
onerror: (e) -> handleClientResponse.call @
+ timeout: s.timeout
+
client.open s.type, s.url, s.async
# Set the correct header, if data is being sent
@@ -333,9 +335,8 @@ module.exports = ($) ->
for key, value of requestHeaders
client.setRequestHeader key, value
-
-
state = 1
+
if options.data
client.send options.data
else
View
26 test/tb/lib/ti/ajax/settings_test.coffee
@@ -571,6 +571,32 @@ describe '$.ajax settings', ->
$.ajax '/test', settings
describe 'timeout', ->
+
+ beforeEach ->
+
+ @delay = Ti.Network.HTTPClient.options.wait
+
+ Ti.Network.HTTPClient.options.wait = 60
+
+ afterEach ->
+ Ti.Network.HTTPClient.options.wait = @delay
+
+ it 'should throw an error on timeout', (done) ->
+
+ settings =
+ timeout: 50
+
+ $.ajax('/test', settings).fail ->
+ done()
+
+ it 'should not throw an error if the response occurs within the timeout', (done) ->
+
+ settings =
+ timeout: 70
+
+ $.ajax('/test', settings).done ->
+ done()
+
describe 'type and method', ->
beforeEach ->
View
9 ti/dist.js
@@ -1026,7 +1026,14 @@ TitaniumHTTPClient = (function() {
}
}
if (this.async && (wait = TitaniumHTTPClient.options.wait)) {
- return setTimeout(handleResponse, wait);
+ if (this.options.timeout < wait) {
+ this.status = 0;
+ return this.options.onerror.call(this, {
+ source: this
+ });
+ } else {
+ return setTimeout(handleResponse, wait);
+ }
} else {
return handleResponse();
}
View
14 ti/src/network/index.coffee
@@ -115,7 +115,19 @@ class TitaniumHTTPClient
@responseHeaders[name] = value
if @async and wait = TitaniumHTTPClient.options.wait
- setTimeout handleResponse, wait
+
+ if @options.timeout < wait
+
+ # console.warn 'error!'
+
+ @status = 0
+
+ @options.onerror.call @,
+ source: @
+
+ else
+ setTimeout handleResponse, wait
+
else
handleResponse()

0 comments on commit f95c8e5

Please sign in to comment.