mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-23 11:37:38 +08:00
added error handler to xhr requests
This commit is contained in:
@@ -87,16 +87,25 @@ ResourceFactory.prototype = {
|
||||
}
|
||||
|
||||
var value = action.isArray ? [] : new Resource(data);
|
||||
self.xhr(action.method, route.url(extend({}, action.params || {}, extractParams(data), params)), data, function(status, response) {
|
||||
if (action.isArray) {
|
||||
foreach(response, function(item){
|
||||
value.push(new Resource(item));
|
||||
});
|
||||
} else {
|
||||
copy(response, value);
|
||||
self.xhr(
|
||||
action.method,
|
||||
route.url(extend({}, action.params || {}, extractParams(data), params)),
|
||||
data,
|
||||
function(status, response) {
|
||||
if (status == 200) {
|
||||
if (action.isArray) {
|
||||
foreach(response, function(item){
|
||||
value.push(new Resource(item));
|
||||
});
|
||||
} else {
|
||||
copy(response, value);
|
||||
}
|
||||
(callback||noop)(value);
|
||||
} else {
|
||||
throw {status: status, response:response, message: status + ": " + response};
|
||||
}
|
||||
}
|
||||
(callback||noop)(value);
|
||||
});
|
||||
);
|
||||
return value;
|
||||
};
|
||||
|
||||
|
||||
@@ -174,7 +174,7 @@ function createScope(parent, services, existing) {
|
||||
}
|
||||
|
||||
function inject(name){
|
||||
var service = getter(servicesCache, name, true), factory, args = [];
|
||||
var service = servicesCache[name], factory, args = [];
|
||||
if (isUndefined(service)) {
|
||||
factory = services[name];
|
||||
if (!isFunction(factory))
|
||||
@@ -182,7 +182,7 @@ function createScope(parent, services, existing) {
|
||||
foreach(factory.inject, function(dependency){
|
||||
args.push(inject(dependency));
|
||||
});
|
||||
setter(servicesCache, name, service = factory.apply(instance, args));
|
||||
servicesCache[name] = service = factory.apply(instance, args);
|
||||
}
|
||||
return service;
|
||||
}
|
||||
|
||||
@@ -196,7 +196,7 @@ angularService('$route', function(location, params){
|
||||
return $route;
|
||||
}, {inject: ['$location']});
|
||||
|
||||
angularService('$xhr', function($browser){
|
||||
angularService('$xhr', function($browser, $error){
|
||||
var self = this;
|
||||
return function(method, url, post, callback){
|
||||
if (isFunction(post)) {
|
||||
@@ -211,15 +211,27 @@ angularService('$xhr', function($browser){
|
||||
if (isString(response) && /^\s*[\[\{]/.exec(response) && /[\}\]]\s*$/.exec(response)) {
|
||||
response = fromJson(response);
|
||||
}
|
||||
callback(code, response);
|
||||
if (code == 200) {
|
||||
callback(code, response);
|
||||
} else {
|
||||
$error(
|
||||
{method: method, url:url, data:post, callback:callback},
|
||||
{status: code, body:response});
|
||||
}
|
||||
} finally {
|
||||
self.$eval();
|
||||
}
|
||||
});
|
||||
};
|
||||
}, {inject:['$browser']});
|
||||
}, {inject:['$browser', '$xhr.error']});
|
||||
|
||||
angularService('$xhr.bulk', function($xhr){
|
||||
angularService('$xhr.error', function($log){
|
||||
return function(request, response){
|
||||
$log.error(response);
|
||||
};
|
||||
}, {inject:['$log']});
|
||||
|
||||
angularService('$xhr.bulk', function($xhr, $error){
|
||||
var requests = [],
|
||||
callbacks = [],
|
||||
scope = this;
|
||||
@@ -254,7 +266,13 @@ angularService('$xhr.bulk', function($xhr){
|
||||
$xhr('POST', url, {requests:currentRequests}, function(code, response){
|
||||
foreach(response, function(response, i){
|
||||
try {
|
||||
(currentCallbacks[i] || noop)(response.status, response.response);
|
||||
if (response.status == 200) {
|
||||
(currentCallbacks[i] || noop)(response.status, response.response);
|
||||
} else {
|
||||
$error(
|
||||
extend({}, currentRequests[i], {callback: currentCallbacks[i]}),
|
||||
{status: response.status, body:response.response});
|
||||
}
|
||||
} catch(e) {
|
||||
scope.$log.error(e);
|
||||
}
|
||||
@@ -267,7 +285,7 @@ angularService('$xhr.bulk', function($xhr){
|
||||
};
|
||||
this.$onEval(PRIORITY_LAST, bulkXHR.flush);
|
||||
return bulkXHR;
|
||||
}, {inject:['$xhr']});
|
||||
}, {inject:['$xhr', '$xhr.error']});
|
||||
|
||||
angularService('$xhr.cache', function($xhr){
|
||||
var inflight = {}, self = this;;
|
||||
|
||||
Reference in New Issue
Block a user