Fixed notifyWhenNoOutstandingRequests() when using JSONP

This commit is contained in:
Misko Hevery 2011-02-03 12:55:23 -08:00
parent 2a9f7b7287
commit d35c1ac8b0
2 changed files with 11 additions and 7 deletions

View file

@ -81,6 +81,7 @@ function Browser(window, document, body, XHR, $log) {
callback = post; callback = post;
post = _null; post = _null;
} }
outstandingRequestCount ++;
if (lowercase(method) == 'json') { if (lowercase(method) == 'json') {
var callbackId = "angular_" + Math.random() + '_' + (idCounter++); var callbackId = "angular_" + Math.random() + '_' + (idCounter++);
callbackId = callbackId.replace(/\d\./, ''); callbackId = callbackId.replace(/\d\./, '');
@ -89,7 +90,7 @@ function Browser(window, document, body, XHR, $log) {
script.src = url.replace('JSON_CALLBACK', callbackId); script.src = url.replace('JSON_CALLBACK', callbackId);
window[callbackId] = function(data){ window[callbackId] = function(data){
window[callbackId] = _undefined; window[callbackId] = _undefined;
callback(200, data); completeOutstandingRequest(callback, 200, data);
}; };
body.append(script); body.append(script);
} else { } else {
@ -98,7 +99,6 @@ function Browser(window, document, body, XHR, $log) {
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Accept", "application/json, text/plain, */*"); xhr.setRequestHeader("Accept", "application/json, text/plain, */*");
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
outstandingRequestCount ++;
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {
if (xhr.readyState == 4) { if (xhr.readyState == 4) {
completeOutstandingRequest(callback, xhr.status || 200, xhr.responseText); completeOutstandingRequest(callback, xhr.status || 200, xhr.responseText);
@ -248,7 +248,7 @@ function Browser(window, document, body, XHR, $log) {
}); });
} }
return listener; return listener;
} };
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
// Cookies API // Cookies API

View file

@ -20,16 +20,16 @@ describe('browser', function(){
fakeWindow = { fakeWindow = {
location: {href:"http://server"}, location: {href:"http://server"},
setTimeout: fakeSetTimeout setTimeout: fakeSetTimeout
} };
var fakeBody = {append: function(node){scripts.push(node)}}; var fakeBody = {append: function(node){scripts.push(node);}};
var fakeXhr = function(){ var fakeXhr = function(){
xhr = this; xhr = this;
this.open = noop; this.open = noop;
this.setRequestHeader = noop; this.setRequestHeader = noop;
this.send = noop; this.send = noop;
} };
logs = {log:[], warn:[], info:[], error:[]}; logs = {log:[], warn:[], info:[], error:[]};
@ -68,15 +68,19 @@ describe('browser', function(){
describe('xhr', function(){ describe('xhr', function(){
describe('JSON', function(){ describe('JSON', function(){
it('should add script tag for request', function() { it('should add script tag for request', function() {
var callback = jasmine.createSpy('callback');
var log = ""; var log = "";
browser.xhr('JSON', 'http://example.org/path?cb=JSON_CALLBACK', function(code, data){ browser.xhr('JSON', 'http://example.org/path?cb=JSON_CALLBACK', function(code, data){
log += code + ':' + data + ';'; log += code + ':' + data + ';';
}); });
browser.notifyWhenNoOutstandingRequests(callback);
expect(callback).not.wasCalled();
expect(scripts.length).toEqual(1); expect(scripts.length).toEqual(1);
var url = scripts[0].src.split('?cb='); var url = scripts[0].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).wasCalled();
expect(log).toEqual('200:data;'); expect(log).toEqual('200:data;');
expect(typeof fakeWindow[url[1]]).toEqual('undefined'); expect(typeof fakeWindow[url[1]]).toEqual('undefined');
}); });
@ -388,7 +392,7 @@ describe('browser', function(){
browser = new Browser(fakeWindow, {}, {}); browser = new Browser(fakeWindow, {}, {});
var events = [], var events = [],
event = {type: "hashchange"} event = {type: "hashchange"};
browser.onHashChange(function(e) { browser.onHashChange(function(e) {
events.push(e); events.push(e);