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

Commit 8a8a2cf

Browse files
committed
refactor($browser.xhr): use $browser.addJs for JSONP
There is no reason why we shouldn't reuse $browser.addJs for JSONP requests.
1 parent 47efe44 commit 8a8a2cf

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

src/Browser.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,12 @@ function Browser(window, document, body, XHR, $log) {
8989
outstandingRequestCount ++;
9090
if (lowercase(method) == 'json') {
9191
var callbackId = ("angular_" + Math.random() + '_' + (idCounter++)).replace(/\d\./, '');
92-
var script = jqLite(rawDocument.createElement('script'))
93-
.attr({type: 'text/javascript', src: url.replace('JSON_CALLBACK', callbackId)});
92+
var script = self.addJs(url.replace('JSON_CALLBACK', callbackId));
9493
window[callbackId] = function(data){
9594
delete window[callbackId];
96-
script.remove();
95+
body[0].removeChild(script)
9796
completeOutstandingRequest(callback, 200, data);
9897
};
99-
body.append(script);
10098
} else {
10199
var xhr = new XHR();
102100
xhr.open(method, url, true);

test/BrowserSpecs.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
describe('browser', function(){
22

3-
var browser, fakeWindow, xhr, logs, scripts, setTimeoutQueue;
3+
var browser, fakeWindow, xhr, logs, scripts, removedScripts, setTimeoutQueue;
44

55
function fakeSetTimeout(fn) {
66
setTimeoutQueue.push(fn);
@@ -17,13 +17,15 @@ describe('browser', function(){
1717
beforeEach(function(){
1818
setTimeoutQueue = [];
1919
scripts = [];
20+
removedScripts = [];
2021
xhr = null;
2122
fakeWindow = {
2223
location: {href:"http://server"},
2324
setTimeout: fakeSetTimeout
2425
};
2526

26-
var fakeBody = {append: function(node){scripts.push(node);}};
27+
var fakeBody = [{appendChild: function(node){scripts.push(node);},
28+
removeChild: function(node){removedScripts.push(node);}}];
2729

2830
var FakeXhr = function(){
2931
xhr = this;
@@ -87,15 +89,13 @@ describe('browser', function(){
8789
expect(callback).not.toHaveBeenCalled();
8890
expect(scripts.length).toEqual(1);
8991
var script = scripts[0];
90-
script.remove = function(){
91-
log += 'remove();';
92-
};
93-
var url = script.attr('src').split('?cb=');
92+
var url = script.src.split('?cb=');
9493
expect(url[0]).toEqual('http://example.org/path');
9594
expect(typeof fakeWindow[url[1]]).toEqual($function);
9695
fakeWindow[url[1]]('data');
9796
expect(callback).toHaveBeenCalled();
98-
expect(log).toEqual('remove();200:data;');
97+
expect(log).toEqual('200:data;');
98+
expect(scripts).toEqual(removedScripts);
9999
expect(fakeWindow[url[1]]).toBeUndefined();
100100
});
101101
});

0 commit comments

Comments
 (0)