added jsonp to resources

This commit is contained in:
Misko Hevery
2010-07-22 11:18:32 -07:00
parent b5bbfaeb80
commit 849a05b5a5
9 changed files with 125 additions and 21 deletions

View File

@@ -1,7 +1,10 @@
var browserSingleton;
angularService('$browser', function browserFactory(){
if (!browserSingleton) {
browserSingleton = new Browser(window.location, window.document);
browserSingleton = new Browser(
window.location,
jqLite(window.document),
jqLite(window.document.getElementsByTagName('head')[0]));
browserSingleton.startUrlWatcher();
browserSingleton.bind();
}

View File

@@ -2,7 +2,7 @@
// Browser
//////////////////////////////
function Browser(location, document) {
function Browser(location, document, head) {
this.delay = 50;
this.expectedUrl = location.href;
this.urlListeners = [];
@@ -21,8 +21,9 @@ function Browser(location, document) {
};
this.location = location;
this.document = jqLite(document);
this.body = jqLite(document.body);
this.document = document;
this.head = head;
this.idCounter = 0;
}
Browser.prototype = {
@@ -58,21 +59,34 @@ Browser.prototype = {
callback = post;
post = null;
}
var xhr = new this.XHR(),
self = this;
xhr.open(method, url, true);
this.outstandingRequests.count ++;
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
try {
callback(xhr.status || 200, xhr.responseText);
} finally {
self.outstandingRequests.count--;
self.processRequestCallbacks();
if (lowercase(method) == 'json') {
var callbackId = "angular_" + Math.random() + '_' + (this.idCounter++);
callbackId = callbackId.replace(/\d\./, '');
var script = this.document[0].createElement('script');
script.type = 'text/javascript';
script.src = url.replace('JSON_CALLBACK', callbackId);
this.head.append(script);
window[callbackId] = function(data){
delete window[callbackId];
callback(200, data);
};
} else {
var xhr = new this.XHR(),
self = this;
xhr.open(method, url, true);
this.outstandingRequests.count ++;
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
try {
callback(xhr.status || 200, xhr.responseText);
} finally {
self.outstandingRequests.count--;
self.processRequestCallbacks();
}
}
}
};
xhr.send(post || '');
};
xhr.send(post || '');
}
},
processRequestCallbacks: function(){

View File

@@ -28,6 +28,7 @@ Route.prototype = {
query.push(encodeURI(key) + '=' + encodeURI(value));
}
});
url = url.replace(/\/*$/, '');
return url + (query.length ? '?' + query.join('&') : '');
}
};
@@ -88,7 +89,7 @@ ResourceFactory.prototype = {
throw "Expected between 0-3 arguments [params, data, callback], got " + arguments.length + " arguments.";
}
var value = action.isArray ? [] : new Resource(data)
var value = action.isArray ? [] : new Resource(data);
self.xhr(
action.method,
route.url(extend({}, action.params || {}, extractParams(data), params)),