vendor/assets/javascripts/angular-resource.js in angular-on-rails-1.0.5 vs vendor/assets/javascripts/angular-resource.js in angular-on-rails-1.1.3u

- old
+ new

@@ -1,7 +1,7 @@ /** - * @license AngularJS v1.0.5 + * @license AngularJS v1.1.3 * (c) 2010-2012 Google, Inc. http://angularjs.org * License: MIT */ (function(window, angular, undefined) { 'use strict'; @@ -23,16 +23,17 @@ * * The returned resource object has action methods which provide high-level behaviors without * the need to interact with the low level {@link ng.$http $http} service. * * @param {string} url A parameterized URL template with parameters prefixed by `:` as in - * `/user/:username`. If you are using a URL with a port number (e.g. + * `/user/:username`. If you are using a URL with a port number (e.g. * `http://example.com:8080/api`), you'll need to escape the colon character before the port * number, like this: `$resource('http://example.com\\:8080/api')`. * * @param {Object=} paramDefaults Default values for `url` parameters. These can be overridden in - * `actions` methods. + * `actions` methods. If any of the parameter value is a function, it will be executed every time + * when a param value needs to be obtained for a request (unless the param was overriden). * * Each key value in the parameter object is first bound to url template if present and then any * excess keys are appended to the url search query after the `?`. * * Given a template `/path/:verb` and parameter `{verb:'greet', salutation:'Hello'}` results in @@ -40,25 +41,44 @@ * * If the parameter value is prefixed with `@` then the value of that parameter is extracted from * the data object (useful for non-GET operations). * * @param {Object.<Object>=} actions Hash with declaration of custom action that should extend the - * default set of resource actions. The declaration should be created in the following format: + * default set of resource actions. The declaration should be created in the format of {@link + * ng.$http#Parameters $http.config}: * - * {action1: {method:?, params:?, isArray:?}, - * action2: {method:?, params:?, isArray:?}, + * {action1: {method:?, params:?, isArray:?, headers:?, ...}, + * action2: {method:?, params:?, isArray:?, headers:?, ...}, * ...} * * Where: * - * - `action` – {string} – The name of action. This name becomes the name of the method on your + * - **`action`** – {string} – The name of action. This name becomes the name of the method on your * resource object. - * - `method` – {string} – HTTP request method. Valid methods are: `GET`, `POST`, `PUT`, `DELETE`, - * and `JSONP` - * - `params` – {object=} – Optional set of pre-bound parameters for this action. - * - isArray – {boolean=} – If true then the returned object for this action is an array, see + * - **`method`** – {string} – HTTP request method. Valid methods are: `GET`, `POST`, `PUT`, `DELETE`, + * and `JSONP`. + * - **`params`** – {Object=} – Optional set of pre-bound parameters for this action. If any of the + * parameter value is a function, it will be executed every time when a param value needs to be + * obtained for a request (unless the param was overriden). + * - **`isArray`** – {boolean=} – If true then the returned object for this action is an array, see * `returns` section. + * - **`transformRequest`** – `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` – + * transform function or an array of such functions. The transform function takes the http + * request body and headers and returns its transformed (typically serialized) version. + * - **`transformResponse`** – `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` – + * transform function or an array of such functions. The transform function takes the http + * response body and headers and returns its transformed (typically deserialized) version. + * - **`cache`** – `{boolean|Cache}` – If true, a default $http cache will be used to cache the + * GET request, otherwise if a cache instance built with + * {@link ng.$cacheFactory $cacheFactory}, this cache will be used for + * caching. + * - **`timeout`** – `{number}` – timeout in milliseconds. + * - **`withCredentials`** - `{boolean}` - whether to to set the `withCredentials` flag on the + * XHR object. See {@link https://developer.mozilla.org/en/http_access_control#section_5 + * requests with credentials} for more information. + * - **`responseType`** - `{string}` - see {@link + * https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#responseType requestType}. * * @returns {Object} A resource "class" object with methods for the default set of resource actions * optionally extended with custom `actions`. The default set contains these actions: * * { 'get': {method:'GET'}, @@ -94,10 +114,28 @@ * - HTTP GET "class" actions: `Resource.action([parameters], [success], [error])` * - non-GET "class" actions: `Resource.action([parameters], postData, [success], [error])` * - non-GET instance actions: `instance.$action([parameters], [success], [error])` * * + * The Resource instances and collection have these additional properties: + * + * - `$then`: the `then` method of a {@link ng.$q promise} derived from the underlying + * {@link ng.$http $http} call. + * + * The success callback for the `$then` method will be resolved if the underlying `$http` requests + * succeeds. + * + * The success callback is called with a single object which is the {@link ng.$http http response} + * object extended with a new property `resource`. This `resource` property is a reference to the + * result of the resource action — resource object or array of resources. + * + * The error callback is called with the {@link ng.$http http response} object when an http + * error occurs. + * + * - `$resolved`: true if the promise has been resolved (either with success or rejection); + * Knowing if the Resource has been resolved is useful in data-binding. + * * @example * * # Credit card resource * * <pre> @@ -136,11 +174,11 @@ * </pre> * * The object returned from this function execution is a resource "class" which has "static" method * for each action in the definition. * - * Calling these methods invoke `$http` on the `url` template with the given `method` and `params`. + * Calling these methods invoke `$http` on the `url` template with the given `method`, `params` and `headers`. * When the data is returned from the server then the object is an instance of the resource type and * all of the non-GET methods are available with `$` prefix. This allows you to easily support CRUD * operations (create, read, update, delete) on server-side data. <pre> @@ -282,11 +320,11 @@ }); this.template = template.replace(/\\:/g, ':'); } Route.prototype = { - url: function(params) { + setUrlParams: function(config, params) { var self = this, url = this.template, val, encodedVal; @@ -305,20 +343,21 @@ return leadingSlashes + tail; } }); } }); - url = url.replace(/\/?#$/, ''); - var query = []; + + // set the url + config.url = url.replace(/\/?#$/, '').replace(/\/*$/, ''); + + // set params - delegate param encoding to $http forEach(params, function(value, key){ if (!self.urlParams[key]) { - query.push(encodeUriQuery(key) + '=' + encodeUriQuery(value)); + config.params = config.params || {}; + config.params[key] = value; } }); - query.sort(); - url = url.replace(/\/*$/, ''); - return url + (query.length ? '?' + query.join('&') : ''); } }; function ResourceFactory(url, paramDefaults, actions) { @@ -328,10 +367,11 @@ function extractParams(data, actionParams){ var ids = {}; actionParams = extend({}, paramDefaults, actionParams); forEach(actionParams, function(value, key){ + if (isFunction(value)) { value = value(); } ids[key] = value.charAt && value.charAt(0) == '@' ? getter(data, value.substr(1)) : value; }); return ids; } @@ -345,10 +385,12 @@ Resource[name] = function(a1, a2, a3, a4) { var params = {}; var data; var success = noop; var error = null; + var promise; + switch(arguments.length) { case 4: error = a4; success = a3; //fallthrough @@ -380,28 +422,48 @@ throw "Expected between 0-4 arguments [params, data, success, error], got " + arguments.length + " arguments."; } var value = this instanceof Resource ? this : (action.isArray ? [] : new Resource(data)); - $http({ - method: action.method, - url: route.url(extend({}, extractParams(data, action.params || {}), params)), - data: data - }).then(function(response) { - var data = response.data; + var httpConfig = {}, + promise; - if (data) { - if (action.isArray) { - value.length = 0; - forEach(data, function(item) { - value.push(new Resource(item)); - }); - } else { - copy(data, value); - } + forEach(action, function(value, key) { + if (key != 'params' && key != 'isArray' ) { + httpConfig[key] = copy(value); + } + }); + httpConfig.data = data; + route.setUrlParams(httpConfig, extend({}, extractParams(data, action.params || {}), params)); + + function markResolved() { value.$resolved = true; } + + promise = $http(httpConfig); + value.$resolved = false; + + promise.then(markResolved, markResolved); + value.$then = promise.then(function(response) { + var data = response.data; + var then = value.$then, resolved = value.$resolved; + + if (data) { + if (action.isArray) { + value.length = 0; + forEach(data, function(item) { + value.push(new Resource(item)); + }); + } else { + copy(data, value); + value.$then = then; + value.$resolved = resolved; } - (success||noop)(value, response.headers); - }, error); + } + + (success||noop)(value, response.headers); + + response.resource = value; + return response; + }, error).then; return value; };