aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMisko Hevery2010-04-29 17:28:33 -0700
committerMisko Hevery2010-04-29 17:28:33 -0700
commitc7913a4b7a3f5ffb0ea6bb1e636ac9d4a0e75c32 (patch)
tree5266b2b7b3f2a3d1dba3a652427e5ce4744bd477 /src
parent913729ee0120cc72e13b18d826c6da0fe2b98bf7 (diff)
downloadangular.js-c7913a4b7a3f5ffb0ea6bb1e636ac9d4a0e75c32.tar.bz2
added $xhr service with bulk and cache, hooked up $resource
Diffstat (limited to 'src')
-rw-r--r--src/Angular.js8
-rw-r--r--src/Browser.js4
-rw-r--r--src/Scope.js4
-rw-r--r--src/services.js87
4 files changed, 92 insertions, 11 deletions
diff --git a/src/Angular.js b/src/Angular.js
index 8eef7275..3af21ced 100644
--- a/src/Angular.js
+++ b/src/Angular.js
@@ -231,12 +231,12 @@ function isLeafNode (node) {
function copy(source, destination){
if (!destination) {
- if (!source) {
- return source;
- } else if (isArray(source)) {
+ if (isArray(source)) {
return copy(source, []);
- } else {
+ } else if (isObject(source)) {
return copy(source, {});
+ } else {
+ return source;
}
} else {
if (isArray(source)) {
diff --git a/src/Browser.js b/src/Browser.js
index 11b079f0..d2e8608d 100644
--- a/src/Browser.js
+++ b/src/Browser.js
@@ -52,12 +52,12 @@ Browser.prototype = {
head.append(link);
},
- xhr: function(method, url, callback){
+ xhr: function(method, url, post, callback){
var xhr = new this.XHR();
xhr.open(method, url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
- callback(xhr.status, xhr.responseText);
+ callback(xhr.status || 200, xhr.responseText);
}
};
xhr.send('');
diff --git a/src/Scope.js b/src/Scope.js
index 54e75dbd..1b93418f 100644
--- a/src/Scope.js
+++ b/src/Scope.js
@@ -173,7 +173,7 @@ function createScope(parent, services, existing) {
}
function inject(name){
- var service = getter(servicesCache, name), factory, args = [];
+ var service = getter(servicesCache, name, true), factory, args = [];
if (isUndefined(service)) {
factory = services[name];
if (!isFunction(factory))
@@ -189,7 +189,7 @@ function createScope(parent, services, existing) {
foreach(services, function(_, name){
var service = inject(name);
if (service) {
- instance[name] = service;
+ setter(instance, name, service);
}
});
diff --git a/src/services.js b/src/services.js
index b854c8d6..36c89ccb 100644
--- a/src/services.js
+++ b/src/services.js
@@ -189,7 +189,88 @@ angularService('$route', function(location, params){
return $route;
}, {inject: ['$location']});
-angularService('$resource', function(browser){
- var resource = new ResourceFactory(bind(browser, browser.xhr));
+angularService('$xhr', function($browser){
+ var self = this;
+ return function(method, url, post, callback){
+ if (post && isObject(post)) {
+ post = toJson(post);
+ }
+ $browser.xhr(method, url, post, function(code, response){
+ try {
+ if (isString(response) && /^\s*[\[\{]/.exec(response) && /[\}\]]\s*$/.exec(response)) {
+ response = fromJson(response);
+ }
+ callback(code, response);
+ } finally {
+ self.$eval();
+ }
+ });
+ };
+}, {inject:['$browser']});
+
+angularService('$xhr.bulk', function($xhr){
+ var requests = [],
+ callbacks = [],
+ scope = this;
+ function bulkXHR(method, url, post, callback) {
+ requests.push({method: method, url: url, data:post});
+ callbacks.push(callback);
+ }
+ bulkXHR.url = "/bulk";
+ bulkXHR.flush = function(callback){
+ var currentRequests = requests,
+ currentCallbacks = callbacks;
+ requests = [];
+ callbacks = [];
+ $xhr('POST', bulkXHR.url, {requests:currentRequests}, function(code, response){
+ foreach(response, function(response, i){
+ try {
+ (currentCallbacks[i] || noop)(response.status, response.response);
+ } catch(e) {
+ self.$log.error(e);
+ }
+ });
+ (callback || noop)();
+ });
+ scope.$eval();
+ };
+ return bulkXHR;
+}, {inject:['$xhr']});
+
+angularService('$xhr.cache', function($xhr){
+ var inflight = {};
+ function cache(method, url, post, callback){
+ if (method == 'GET') {
+ var data;
+ if (data = cache.data[url]) {
+ callback(200, copy(data.value));
+ } else if (data = inflight[url]) {
+ data.callbacks.push(callback);
+ } else {
+ inflight[url] = {callbacks: [callback]};
+ cache.delegate(method, url, post, function(status, response){
+ if (status == 200)
+ cache.data[url] = { value: response };
+ foreach(inflight[url].callbacks, function(callback){
+ try {
+ (callback||noop)(status, copy(response));
+ } catch(e) {
+ self.$log.error(e);
+ }
+ });
+ delete inflight[url];
+ });
+ }
+ } else {
+ cache.delegate(method, url, post, callback);
+ }
+ }
+ cache.data = {};
+ cache.delegate = $xhr;
+ return cache;
+}, {inject:['$xhr']});
+
+angularService('$resource', function($xhr){
+ var resource = new ResourceFactory($xhr);
return bind(resource, resource.route);
-}, {inject: ['$browser']});
+}, {inject: ['$xhr.cache']});