diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Resource.js | 7 | ||||
| -rw-r--r-- | src/service/http.js | 102 | ||||
| -rw-r--r-- | src/widgets.js | 15 |
3 files changed, 85 insertions, 39 deletions
diff --git a/src/Resource.js b/src/Resource.js index 4bec60f9..f4440e00 100644 --- a/src/Resource.js +++ b/src/Resource.js @@ -111,7 +111,8 @@ ResourceFactory.prototype = { method: action.method, url: route.url(extend({}, extractParams(data), action.params || {}, params)), data: data - }).on('success', function(response, status) { + }).then(function(response) { + response = response.data; if (response) { if (action.isArray) { value.length = 0; @@ -123,9 +124,7 @@ ResourceFactory.prototype = { } } (success||noop)(value); - }); - - if (error) future.on('error', error); + }, error); return value; }; diff --git a/src/service/http.js b/src/service/http.js index 8458617b..5666f486 100644 --- a/src/service/http.js +++ b/src/service/http.js @@ -48,18 +48,6 @@ function transform(data, fns, param) { } -/** - * @ngdoc object - * @name angular.module.ng.$http - * @requires $httpBacked - * @requires $browser - * @requires $exceptionHandler - * @requires $cacheFactory - * - * @property {Array.<XhrFuture>} pendingRequests Array of pending requests. - * - * @description - */ function $HttpProvider() { var JSON_START = /^\s*(\[|\{[^\{])/, JSON_END = /[\}\]]\s*$/, @@ -93,14 +81,75 @@ function $HttpProvider() { } }; - this.$get = ['$httpBackend', '$browser', '$exceptionHandler', '$cacheFactory', '$rootScope', - function($httpBackend, $browser, $exceptionHandler, $cacheFactory, $rootScope) { + this.$get = ['$httpBackend', '$browser', '$exceptionHandler', '$cacheFactory', '$rootScope', '$q', + function($httpBackend, $browser, $exceptionHandler, $cacheFactory, $rootScope, $q) { var defaultCache = $cacheFactory('$http'); - // the actual service + /** + * @ngdoc function + * @name angular.module.ng.$http + * @requires $httpBacked + * @requires $browser + * @requires $exceptionHandler + * @requires $cacheFactory + * + * @param {object} config Object describing the request to be made and how it should be processed. + * The object has following properties: + * + * - **method** – `{string}` – HTTP method (e.g. 'GET', 'POST', etc) + * - **url** – `{string}` – Absolute or relative URL of the resource that is being requested. + * - **data** – `{string|Object}` – Data to be sent as the request message data. + * - **headers** – `{Object}` – Map of strings representing HTTP headers to send to the server. + * - **cache** – `{boolean|Cache}` – If true, a default $http cache will be used to cache the + * GET request, otherwise if a cache instance built with $cacheFactory, this cache will be + * used for caching. + * + * @returns {HttpPromise} Returns a promise object with the standard `then` method and two http + * specific methods: `success` and `error`. The `then` method takes two arguments a success and + * an error callback which will be called with a response object. The `success` and `error` + * methods take a single argument - a function that will be called when the request succeeds or + * fails respectively. The arguments passed into these functions are destructured representation + * of the response object passed into the `then` method. The response object has these + * properties: + * + * - **data** – `{string|Object}` – The response body transformed with the transform functions. + * - **status** – `{number}` – HTTP status code of the response. + * - **headers** – `{function([headerName])}` – Header getter function. + * - **config** – `{Object}` – The configuration object that was used to generate the request. + * + * @property {Array.<Object>} pendingRequests Array of config objects for pending requests. + * This is primarily meant to be used for debugging purposes. + * + * @description + * $http is a service through which XHR and JSONP requests can be made. + */ function $http(config) { - return new XhrFuture().send(config); + var req = new XhrFuture().send(config), + deferredResp = $q.defer(), + promise = deferredResp.promise; + + promise.success = function(fn) { + promise.then(function(response) { + fn(response.data, response.status, response.headers, config); + }); + return promise; + }; + + promise.error = function(fn) { + promise.then(null, function(response) { + fn(response.data, response.status, response.headers, config); + }); + return promise; + }; + + req.on('success', function(data, status, headers) { + deferredResp.resolve({data: data, status: status, headers: headers, config: config}); + }).on('error', function(data, status, headers) { + deferredResp.reject({data: data, status: status, headers: headers, config: config}); + }); + + return promise; } $http.pendingRequests = []; @@ -115,7 +164,7 @@ function $HttpProvider() { * * @param {string} url Relative or absolute URL specifying the destination of the request * @param {Object=} config Optional configuration object - * @returns {XhrFuture} Future object + * @returns {HttpPromise} Future object */ /** @@ -128,7 +177,7 @@ function $HttpProvider() { * * @param {string} url Relative or absolute URL specifying the destination of the request * @param {Object=} config Optional configuration object - * @returns {XhrFuture} Future object + * @returns {HttpPromise} Future object */ /** @@ -154,7 +203,7 @@ function $HttpProvider() { * * @param {string} url Relative or absolute URL specifying the destination of the request * @param {Object=} config Optional configuration object - * @returns {XhrFuture} Future object + * @returns {HttpPromise} Future object */ /** @@ -183,7 +232,7 @@ function $HttpProvider() { * @param {string} url Relative or absolute URL specifying the destination of the request * @param {*} data Request content * @param {Object=} config Optional configuration object - * @returns {XhrFuture} Future object + * @returns {HttpPromise} Future object */ /** @@ -260,7 +309,9 @@ function $HttpProvider() { } fireCallbacks(response, status); - rawRequest = null; + // TODO(i): we can't null the rawRequest because we might need to be able to call + // rawRequest.getAllResponseHeaders from a promise + // rawRequest = null; } /** @@ -284,7 +335,7 @@ function $HttpProvider() { response = transform(response, cfg.transformResponse || $config.transformResponse, rawRequest); var idx; // remove from pending requests - if ((idx = indexOf($http.pendingRequests, self)) !== -1) + if ((idx = indexOf($http.pendingRequests, cfg)) !== -1) $http.pendingRequests.splice(idx, 1); // normalize internal statuses to 0 @@ -331,7 +382,7 @@ function $HttpProvider() { * Retry the request * * @param {Object=} config Optional config object to extend the original configuration - * @returns {XhrFuture} + * @returns {HttpPromise} */ this.retry = function(config) { if (rawRequest) throw 'Can not retry request. Abort pending request first.'; @@ -377,8 +428,7 @@ function $HttpProvider() { rawRequest = $httpBackend(cfg.method, cfg.url, data, done, headers, cfg.timeout); } - $rootScope.$broadcast('$http.request', self); - $http.pendingRequests.push(self); + $http.pendingRequests.push(cfg); return self; }; @@ -416,7 +466,7 @@ function $HttpProvider() { * .on('abort', function(){}); * * @param {string} pattern Status code pattern with "x" for any number - * @param {function(*, number, Object)} callback Function to be called when response arrives + * @param {function(*, number, function)} callback Function to be called when response arrives * @returns {XhrFuture} */ this.on = function(pattern, callback) { diff --git a/src/widgets.js b/src/widgets.js index c89f4179..618de04d 100644 --- a/src/widgets.js +++ b/src/widgets.js @@ -109,8 +109,7 @@ angularWidget('ng:include', function(element){ }); this.$watch(function() {return changeCounter;}, function(scope) { var src = scope.$eval(srcExp), - useScope = scope.$eval(scopeExp), - fromCache; + useScope = scope.$eval(scopeExp); function clearContent() { childScope = null; @@ -121,7 +120,7 @@ angularWidget('ng:include', function(element){ releaseScopes.pop().$destroy(); } if (src) { - $http.get(src, {cache: $templateCache}).on('success', function(response) { + $http.get(src, {cache: $templateCache}).success(function(response) { element.html(response); if (useScope) { childScope = useScope; @@ -131,7 +130,7 @@ angularWidget('ng:include', function(element){ compiler.compile(element)(childScope); $autoScroll(); scope.$eval(onloadExp); - }).on('error', clearContent); + }).error(clearContent); } else { clearContent(); } @@ -572,23 +571,21 @@ angularWidget('ng:view', function(element) { }); this.$watch(function() {return changeCounter;}, function(scope, newChangeCounter) { - var template = $route.current && $route.current.template, - fromCache; + var template = $route.current && $route.current.template; function clearContent() { element.html(''); } if (template) { - // xhr's callback must be async, see commit history for more info - $http.get(template, {cache: $templateCache}).on('success', function(response) { + $http.get(template, {cache: $templateCache}).success(function(response) { // ignore callback if another route change occured since if (newChangeCounter == changeCounter) { element.html(response); compiler.compile(element)($route.current.scope); $autoScroll(); } - }).on('error', clearContent); + }).error(clearContent); } else { clearContent(); } |
