Skip to content

Commit

Permalink
jsonp now accepts a callback param, generally more robust, fixes #137
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Graul authored and Irene Ros committed Jun 13, 2012
1 parent 676e162 commit 92c3ce6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 9 deletions.
25 changes: 18 additions & 7 deletions src/importers/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
this.params = {
type : "GET",
url : _.isFunction(this._url) ? _.bind(this._url, this) : this._url,
dataType : options.dataType ? options.dataType : (options.jsonp ? "jsonp" : "json")
dataType : options.dataType ? options.dataType : (options.jsonp ? "jsonp" : "json"),
callback : options.callback
};
};

Expand Down Expand Up @@ -75,7 +76,8 @@
url,
options.success,
options.dataType === "script",
options.error
options.error,
options.callback
);

return;
Expand Down Expand Up @@ -103,7 +105,7 @@
}
};

Miso.Xhr.getJSONP = function(url, success, isScript, error) {
Miso.Xhr.getJSONP = function(url, success, isScript, error, callback) {
// If this is a script request, ensure that we do not
// call something that has already been loaded
if (isScript) {
Expand All @@ -130,7 +132,7 @@
paramStr = url.split("?")[ 1 ],
isFired = false,
params = [],
callback, parts, callparam;
parts;

// Extract params
if (paramStr && !isScript) {
Expand All @@ -139,10 +141,17 @@
if (params.length) {
parts = params[params.length - 1].split("=");
}
callback = params.length ? (parts[ 1 ] ? parts[ 1 ] : parts[ 0 ]) : "jsonp";
if (!callback) {
callback = params.length ? (parts[ 1 ] ? parts[ 1 ] : parts[ 0 ]) : "jsonp";
}

if (!paramStr && !isScript) {
url += "?callback=" + callback;
url += "?"
}

if ( !paramStr || !/callback/.test(paramStr) ) {
if (paramStr) { url += '&'; }
url += "callback=" + callback;
}

if (callback && !isScript) {
Expand All @@ -161,7 +170,9 @@
};

// Replace callback param and callback name
url = url.replace(parts.join("="), parts[0] + "=" + callback);
if (parts) {
url = url.replace(parts.join("="), parts[0] + "=" + callback);
}
}

script.onload = script.onreadystatechange = function() {
Expand Down
73 changes: 71 additions & 2 deletions test/unit/importers.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,42 @@
test("Basic jsonp url fetch with Dataset API", 46, function() {
var url = "data/alphabet_obj.json?callback=";
var ds = new Miso.Dataset({
url : url,
jsonp : true,
url : url,
jsonp : true,
extract: function(raw) {
return raw.data;
},
ready : function() {
verifyImport({}, this);
start();
}
});
ds.fetch();
stop();
});

test("Basic jsonp url fetch with Dataset API without setting callback=", 46, function() {
var url = "data/alphabet_obj.json";
var ds = new Miso.Dataset({
url : url,
jsonp : true,
extract: function(raw) {
return raw.data;
},
ready : function() {
verifyImport({}, this);
start();
}
});
ds.fetch();
stop();
});

test("Basic jsonp url fetch with Dataset API setting a callback in the url", 46, function() {
var url = "data/alphabet_obj.json?callback=testing";
var ds = new Miso.Dataset({
url : url,
jsonp : true,
extract: function(raw) {
return raw.data;
},
Expand All @@ -182,6 +216,41 @@
stop();
});

test("Basic jsonp url fetch with Dataset API without setting callback param but with other params", 46, function() {
var url = "data/alphabet_obj.json?a=b";
var ds = new Miso.Dataset({
url : url,
jsonp : true,
extract: function(raw) {
return raw.data;
},
ready : function() {
verifyImport({}, this);
start();
}
});
ds.fetch();
stop();
});

test("Basic jsonp url fetch with Dataset API & custom callback", 46, function() {
var url = "data/alphabet_obj.json?callback=";
var ds = new Miso.Dataset({
url : url,
jsonp : true,
extract: function(raw) {
return raw.data;
},
ready : function() {
verifyImport({}, this);
start();
},
callback: 'foobar'
});
ds.fetch();
stop();
});


test("Basic delimiter parsing test with Dataset API", 46, function() {

Expand Down

0 comments on commit 92c3ce6

Please sign in to comment.