From a8908a147ee2df02562548e9c792ab107c646ef3 Mon Sep 17 00:00:00 2001 From: Ricardo Bin Date: Fri, 9 Nov 2012 18:54:21 -0200 Subject: [PATCH 1/3] JSONP requests with the same callback name --- src/ng/httpBackend.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ng/httpBackend.js b/src/ng/httpBackend.js index bca46ee1a939..d2c29f969db4 100644 --- a/src/ng/httpBackend.js +++ b/src/ng/httpBackend.js @@ -37,12 +37,12 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument, url = url || $browser.url(); if (lowercase(method) == 'jsonp') { - var callbackId = '_' + (callbacks.counter++).toString(36); + var callbackId = url.indexOf('JSON_SINGLE_CALLBACK') === -1 ? '_' + (callbacks.counter++).toString(36) : 'single'; callbacks[callbackId] = function(data) { callbacks[callbackId].data = data; }; - jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId), + jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId).replace('JSON_SINGLE_CALLBACK', 'angular.callbacks.single'), function() { if (callbacks[callbackId].data) { completeRequest(callback, 200, callbacks[callbackId].data); From 6af2429afedbcbef77c134c17f1fcf3d21cfa7ac Mon Sep 17 00:00:00 2001 From: Ricardo Bin Date: Fri, 9 Nov 2012 18:54:21 -0200 Subject: [PATCH 2/3] feat($http) - JSONP requests with the same callback name --- src/ng/httpBackend.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ng/httpBackend.js b/src/ng/httpBackend.js index bca46ee1a939..d2c29f969db4 100644 --- a/src/ng/httpBackend.js +++ b/src/ng/httpBackend.js @@ -37,12 +37,12 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument, url = url || $browser.url(); if (lowercase(method) == 'jsonp') { - var callbackId = '_' + (callbacks.counter++).toString(36); + var callbackId = url.indexOf('JSON_SINGLE_CALLBACK') === -1 ? '_' + (callbacks.counter++).toString(36) : 'single'; callbacks[callbackId] = function(data) { callbacks[callbackId].data = data; }; - jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId), + jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId).replace('JSON_SINGLE_CALLBACK', 'angular.callbacks.single'), function() { if (callbacks[callbackId].data) { completeRequest(callback, 200, callbacks[callbackId].data); From a084553425c50213337aafecee3d6a1783bf9d7b Mon Sep 17 00:00:00 2001 From: Ricardo Bin Date: Sun, 11 Nov 2012 20:41:35 -0200 Subject: [PATCH 3/3] test($http) - unit tests to $http.jsonp and JSON_SINGLE_CALLBACK --- test/ng/httpBackendSpec.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/ng/httpBackendSpec.js b/test/ng/httpBackendSpec.js index a491ae269870..e3d920adc098 100644 --- a/test/ng/httpBackendSpec.js +++ b/test/ng/httpBackendSpec.js @@ -235,6 +235,31 @@ describe('$httpBackend', function() { expect(fakeDocument.$$scripts[0].src).toBe($browser.url()); }); + it('should always call callback with the same name when use JSON_SINGLE_CALLBACK', function() { + callback.andCallFake(function(status, response) { + expect(status).toBe(200); + expect(response).toBe('some-data'); + }); + + $backend('JSONP', 'http://example.org/path?cb=JSON_SINGLE_CALLBACK', null, callback); + expect(fakeDocument.$$scripts.length).toBe(1); + + var script = fakeDocument.$$scripts.shift(), + url = script.src.match(/([^\?]*)\?cb=angular\.callbacks\.single/); + + expect(url[1]).toBe('http://example.org/path'); + callbacks['single']('some-data'); + + if (script.onreadystatechange) { + script.readyState = 'complete'; + script.onreadystatechange(); + } else { + script.onload() + } + + expect(callback).toHaveBeenCalledOnce(); + }); + // TODO(vojta): test whether it fires "async-start" // TODO(vojta): test whether it fires "async-end" on both success and error