Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow simultaneous jsonp requests #171

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions reqwest.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
, head = doc[byTag]('head')[0]
, uniqid = 0
, callbackPrefix = 'reqwest_' + (+new Date())
, lastValue // data stored by the most recent JSONP callback
, lastValue = {} // data collection stored by the most recent JSONP callback
, xmlHttpRequest = 'XMLHttpRequest'
, xDomainRequest = 'XDomainRequest'
, noop = function () {}
Expand Down Expand Up @@ -112,24 +112,32 @@
}
}

function generalCallback(data) {
lastValue = data
}

function urlappend (url, s) {
return url + (/\?/.test(url) ? '&' : '?') + s
}

function handleJsonp(o, fn, err, url) {
var reqId = uniqid++
, cbkey = o['jsonpCallback'] || 'callback' // the 'callback' key
, cbval = o['jsonpCallbackName'] || reqwest.getcallbackPrefix(reqId)
, cbval = o['jsonpCallbackName'] || reqwest.generateUniqueCallbackName(reqId)
, cbreg = new RegExp('((^|\\?|&)' + cbkey + ')=([^&]+)')
, match = url.match(cbreg)
, script = doc.createElement('script')
, loaded = 0
, isIE10 = navigator.userAgent.indexOf('MSIE 10.0') !== -1

var jsonpCallback = function(data) {
lastValue[reqId] = data
},
removeProcessedJsonpData = function() {
if (lastValue.hasOwnProperty(reqId)) {
delete lastValue[reqId]
}
if (cbval.indexOf(reqwest.getcallbackPrefix()) >= 0 && win.hasOwnProperty(cbval)) {
delete win[cbval]
}
}

if (match) {
if (match[3] === '?') {
url = url.replace(cbreg, '$1=' + cbval) // wildcard callback func name
Expand All @@ -140,7 +148,7 @@
url = urlappend(url, cbkey + '=' + cbval) // no callback details, add 'em
}

win[cbval] = generalCallback
win[cbval] = jsonpCallback

script.type = 'text/javascript'
script.src = url
Expand All @@ -159,8 +167,8 @@
script.onload = script.onreadystatechange = null
script.onclick && script.onclick()
// Call the user callback with the last value stored and clean up values and scripts.
fn(lastValue)
lastValue = undefined
fn(lastValue[reqId])
removeProcessedJsonpData()
head.removeChild(script)
loaded = 1
}
Expand All @@ -173,7 +181,7 @@
abort: function () {
script.onload = script.onreadystatechange = null
err({}, 'Request is aborted: timeout', {})
lastValue = undefined
removeProcessedJsonpData()
head.removeChild(script)
loaded = 1
}
Expand Down Expand Up @@ -299,7 +307,7 @@

function success (resp) {
var type = o['type'] || resp && setType(resp.getResponseHeader('Content-Type')) // resp can be undefined in IE
resp = (type !== 'jsonp') ? self.request : resp
resp = (type !== 'jsonp') ? self.request : (resp || {})
// use global data filter on response text
var filteredResponse = globalSetupOptions.dataFilter(resp.responseText, type)
, r = filteredResponse
Expand Down Expand Up @@ -592,6 +600,14 @@
return callbackPrefix
}

reqwest.generateUniqueCallbackName = function (reqId) {
var separator = '_'
//callback name should look like reqwest_1416364846406_1416364865317_2
return this.getcallbackPrefix() + separator +
(+new Date()) + separator +
reqId
}

// jQuery and Zepto compatibility, differences can be remapped here so you can call
// .ajax.compat(options, callback)
reqwest.compat = function (o, fn) {
Expand Down
Loading