Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat($http): setting JSONP callback name #3073

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 8 additions & 3 deletions src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
url = url || $browser.url();

if (lowercase(method) == 'jsonp') {
var callbackId = '_' + (callbacks.counter++).toString(36);
var callbackId = (/JSON_CALLBACK\((.*)\)/).exec(url) || ('_' + (callbacks.counter++).toString(36));

if (callbackId.constructor.name === "Array") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would reword it more like this:

var base = (/JSON_CALLBACK\((.*?)\)/).exec(url);
base = base ? base[1] : '';
callbackId = base + ('_' + (callbacks.counter++).toString(36));

This way we use the nam

callbackId = callbackId[1];
}

callbacks[callbackId] = function(data) {
callbacks[callbackId].data = data;
};

var jsonpDone = jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing whitespace

var jsonpDone = jsonpReq(url.replace(/JSON_CALLBACK\((.*)\)|JSON_CALLBACK/, 'angular.callbacks.' + callbackId),
function() {
if (callbacks[callbackId].data) {
completeRequest(callback, 200, callbacks[callbackId].data);
Expand Down
8 changes: 8 additions & 0 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,14 @@ describe('$httpBackend', function() {
expect(callback).toHaveBeenCalledOnce();
});

it('should set the callback name when use JSONP_CALLBACK with a parameter', function() {
$backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK(someCallback)', null, callback);
var script = fakeDocument.$$scripts.shift(),
url = script.src.match(SCRIPT_URL);

expect(url[1]).toBe('http://example.org/path');
expect(url[2]).toBe('someCallback');
});

// TODO(vojta): test whether it fires "async-start"
// TODO(vojta): test whether it fires "async-end" on both success and error
Expand Down