refactor($browser.xhr): use $browser.addJs for JSONP

There is no reason why we shouldn't reuse $browser.addJs for JSONP
requests.
This commit is contained in:
Igor Minar 2011-07-12 00:47:07 -07:00
parent 47efe44a1d
commit 8a8a2cf462
2 changed files with 9 additions and 11 deletions

View file

@ -89,14 +89,12 @@ function Browser(window, document, body, XHR, $log) {
outstandingRequestCount ++; outstandingRequestCount ++;
if (lowercase(method) == 'json') { if (lowercase(method) == 'json') {
var callbackId = ("angular_" + Math.random() + '_' + (idCounter++)).replace(/\d\./, ''); var callbackId = ("angular_" + Math.random() + '_' + (idCounter++)).replace(/\d\./, '');
var script = jqLite(rawDocument.createElement('script')) var script = self.addJs(url.replace('JSON_CALLBACK', callbackId));
.attr({type: 'text/javascript', src: url.replace('JSON_CALLBACK', callbackId)});
window[callbackId] = function(data){ window[callbackId] = function(data){
delete window[callbackId]; delete window[callbackId];
script.remove(); body[0].removeChild(script)
completeOutstandingRequest(callback, 200, data); completeOutstandingRequest(callback, 200, data);
}; };
body.append(script);
} else { } else {
var xhr = new XHR(); var xhr = new XHR();
xhr.open(method, url, true); xhr.open(method, url, true);

View file

@ -1,6 +1,6 @@
describe('browser', function(){ describe('browser', function(){
var browser, fakeWindow, xhr, logs, scripts, setTimeoutQueue; var browser, fakeWindow, xhr, logs, scripts, removedScripts, setTimeoutQueue;
function fakeSetTimeout(fn) { function fakeSetTimeout(fn) {
setTimeoutQueue.push(fn); setTimeoutQueue.push(fn);
@ -17,13 +17,15 @@ describe('browser', function(){
beforeEach(function(){ beforeEach(function(){
setTimeoutQueue = []; setTimeoutQueue = [];
scripts = []; scripts = [];
removedScripts = [];
xhr = null; xhr = null;
fakeWindow = { fakeWindow = {
location: {href:"http://server"}, location: {href:"http://server"},
setTimeout: fakeSetTimeout setTimeout: fakeSetTimeout
}; };
var fakeBody = {append: function(node){scripts.push(node);}}; var fakeBody = [{appendChild: function(node){scripts.push(node);},
removeChild: function(node){removedScripts.push(node);}}];
var FakeXhr = function(){ var FakeXhr = function(){
xhr = this; xhr = this;
@ -87,15 +89,13 @@ describe('browser', function(){
expect(callback).not.toHaveBeenCalled(); expect(callback).not.toHaveBeenCalled();
expect(scripts.length).toEqual(1); expect(scripts.length).toEqual(1);
var script = scripts[0]; var script = scripts[0];
script.remove = function(){ var url = script.src.split('?cb=');
log += 'remove();';
};
var url = script.attr('src').split('?cb=');
expect(url[0]).toEqual('http://example.org/path'); expect(url[0]).toEqual('http://example.org/path');
expect(typeof fakeWindow[url[1]]).toEqual($function); expect(typeof fakeWindow[url[1]]).toEqual($function);
fakeWindow[url[1]]('data'); fakeWindow[url[1]]('data');
expect(callback).toHaveBeenCalled(); expect(callback).toHaveBeenCalled();
expect(log).toEqual('remove();200:data;'); expect(log).toEqual('200:data;');
expect(scripts).toEqual(removedScripts);
expect(fakeWindow[url[1]]).toBeUndefined(); expect(fakeWindow[url[1]]).toBeUndefined();
}); });
}); });