2011-07-17 08:05:43 +00:00
|
|
|
|
'use strict';
|
|
|
|
|
|
|
2011-02-15 06:12:45 +00:00
|
|
|
|
/**
|
2012-03-27 04:18:01 +00:00
|
|
|
|
* @ngdoc overview
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* @name ngResource
|
2012-03-27 04:18:01 +00:00
|
|
|
|
* @description
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2012-11-25 15:10:37 +00:00
|
|
|
|
/**
|
2011-11-10 06:23:36 +00:00
|
|
|
|
* @ngdoc object
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* @name ngResource.$resource
|
2011-10-19 17:47:17 +00:00
|
|
|
|
* @requires $http
|
2011-02-15 06:12:45 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @description
|
2011-02-14 18:17:04 +00:00
|
|
|
|
* A factory which creates a resource object that lets you interact with
|
2011-02-15 06:12:45 +00:00
|
|
|
|
* [RESTful](http://en.wikipedia.org/wiki/Representational_State_Transfer) server-side data sources.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The returned resource object has action methods which provide high-level behaviors without
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* the need to interact with the low level {@link ng.$http $http} service.
|
2011-02-15 06:12:45 +00:00
|
|
|
|
*
|
2013-03-05 03:04:18 +00:00
|
|
|
|
* # Installation
|
2013-05-08 22:08:49 +00:00
|
|
|
|
* To use $resource make sure you have included the `angular-resource.js` that comes in Angular
|
2013-04-04 15:57:14 +00:00
|
|
|
|
* package. You can also find this file on Google CDN, bower as well as at
|
|
|
|
|
|
* {@link http://code.angularjs.org/ code.angularjs.org}.
|
|
|
|
|
|
*
|
2013-03-05 03:04:18 +00:00
|
|
|
|
* Finally load the module in your application:
|
|
|
|
|
|
*
|
|
|
|
|
|
* angular.module('app', ['ngResource']);
|
|
|
|
|
|
*
|
2013-04-04 15:57:14 +00:00
|
|
|
|
* and you are ready to get started!
|
2013-03-05 03:04:18 +00:00
|
|
|
|
*
|
2013-03-21 19:09:47 +00:00
|
|
|
|
* @param {string} url A parametrized URL template with parameters prefixed by `:` as in
|
2013-02-12 05:43:30 +00:00
|
|
|
|
* `/user/:username`. If you are using a URL with a port number (e.g.
|
2012-12-07 18:09:42 +00:00
|
|
|
|
* `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')`.
|
2011-02-15 06:12:45 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @param {Object=} paramDefaults Default values for `url` parameters. These can be overridden in
|
2012-07-04 08:07:09 +00:00
|
|
|
|
* `actions` methods. If any of the parameter value is a function, it will be executed every time
|
2013-03-21 19:09:47 +00:00
|
|
|
|
* when a param value needs to be obtained for a request (unless the param was overridden).
|
2011-02-15 06:12:45 +00:00
|
|
|
|
*
|
|
|
|
|
|
* 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
|
|
|
|
|
|
* URL `/path/greet?salutation=Hello`.
|
|
|
|
|
|
*
|
|
|
|
|
|
* 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
|
2013-01-18 06:01:50 +00:00
|
|
|
|
* default set of resource actions. The declaration should be created in the format of {@link
|
|
|
|
|
|
* ng.$http#Parameters $http.config}:
|
2011-02-15 06:12:45 +00:00
|
|
|
|
*
|
2013-01-18 06:01:50 +00:00
|
|
|
|
* {action1: {method:?, params:?, isArray:?, headers:?, ...},
|
|
|
|
|
|
* action2: {method:?, params:?, isArray:?, headers:?, ...},
|
2011-02-15 06:12:45 +00:00
|
|
|
|
* ...}
|
|
|
|
|
|
*
|
|
|
|
|
|
* Where:
|
|
|
|
|
|
*
|
2013-01-18 06:01:50 +00:00
|
|
|
|
* - **`action`** – {string} – The name of action. This name becomes the name of the method on your
|
2011-02-15 06:12:45 +00:00
|
|
|
|
* resource object.
|
2013-01-18 06:01:50 +00:00
|
|
|
|
* - **`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
|
2013-03-21 19:09:47 +00:00
|
|
|
|
* obtained for a request (unless the param was overridden).
|
2013-02-22 02:49:26 +00:00
|
|
|
|
* - **`url`** – {string} – action specific `url` override. The url templating is supported just like
|
|
|
|
|
|
* for the resource-level urls.
|
2013-01-18 06:01:50 +00:00
|
|
|
|
* - **`isArray`** – {boolean=} – If true then the returned object for this action is an array, see
|
2011-02-15 06:12:45 +00:00
|
|
|
|
* `returns` section.
|
2013-01-18 06:01:50 +00:00
|
|
|
|
* - **`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}.
|
2011-02-15 06:12:45 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @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'},
|
|
|
|
|
|
* 'save': {method:'POST'},
|
|
|
|
|
|
* 'query': {method:'GET', isArray:true},
|
|
|
|
|
|
* 'remove': {method:'DELETE'},
|
|
|
|
|
|
* 'delete': {method:'DELETE'} };
|
|
|
|
|
|
*
|
2012-06-12 06:49:24 +00:00
|
|
|
|
* Calling these methods invoke an {@link ng.$http} with the specified http method,
|
2011-02-15 06:12:45 +00:00
|
|
|
|
* destination and parameters. When the data is returned from the server then the object is an
|
2013-02-10 19:40:23 +00:00
|
|
|
|
* instance of the resource class. The actions `save`, `remove` and `delete` are available on it
|
|
|
|
|
|
* as methods with the `$` prefix. This allows you to easily perform CRUD operations (create,
|
|
|
|
|
|
* read, update, delete) on server-side data like this:
|
2011-02-15 06:12:45 +00:00
|
|
|
|
* <pre>
|
|
|
|
|
|
var User = $resource('/user/:userId', {userId:'@id'});
|
2011-10-07 18:27:49 +00:00
|
|
|
|
var user = User.get({userId:123}, function() {
|
2011-02-15 06:12:45 +00:00
|
|
|
|
user.abc = true;
|
|
|
|
|
|
user.$save();
|
|
|
|
|
|
});
|
|
|
|
|
|
</pre>
|
|
|
|
|
|
*
|
|
|
|
|
|
* It is important to realize that invoking a $resource object method immediately returns an
|
|
|
|
|
|
* empty reference (object or array depending on `isArray`). Once the data is returned from the
|
|
|
|
|
|
* server the existing reference is populated with the actual data. This is a useful trick since
|
|
|
|
|
|
* usually the resource is assigned to a model which is then rendered by the view. Having an empty
|
|
|
|
|
|
* object results in no rendering, once the data arrives from the server then the object is
|
|
|
|
|
|
* populated with the data and the view automatically re-renders itself showing the new data. This
|
|
|
|
|
|
* means that in most case one never has to write a callback function for the action methods.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The action methods on the class object or instance object can be invoked with the following
|
|
|
|
|
|
* parameters:
|
|
|
|
|
|
*
|
2011-07-22 19:56:45 +00:00
|
|
|
|
* - HTTP GET "class" actions: `Resource.action([parameters], [success], [error])`
|
2011-12-28 21:02:09 +00:00
|
|
|
|
* - non-GET "class" actions: `Resource.action([parameters], postData, [success], [error])`
|
2011-07-22 19:56:45 +00:00
|
|
|
|
* - non-GET instance actions: `instance.$action([parameters], [success], [error])`
|
2011-02-15 06:12:45 +00:00
|
|
|
|
*
|
2013-01-17 22:21:35 +00:00
|
|
|
|
*
|
2013-02-12 05:43:30 +00:00
|
|
|
|
* 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);
|
2013-01-17 22:21:35 +00:00
|
|
|
|
* Knowing if the Resource has been resolved is useful in data-binding.
|
2011-02-15 06:12:45 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @example
|
|
|
|
|
|
*
|
|
|
|
|
|
* # Credit card resource
|
|
|
|
|
|
*
|
|
|
|
|
|
* <pre>
|
|
|
|
|
|
// Define CreditCard class
|
|
|
|
|
|
var CreditCard = $resource('/user/:userId/card/:cardId',
|
|
|
|
|
|
{userId:123, cardId:'@id'}, {
|
|
|
|
|
|
charge: {method:'POST', params:{charge:true}}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// We can retrieve a collection from the server
|
2012-04-15 16:42:54 +00:00
|
|
|
|
var cards = CreditCard.query(function() {
|
|
|
|
|
|
// GET: /user/123/card
|
|
|
|
|
|
// server returns: [ {id:456, number:'1234', name:'Smith'} ];
|
2011-02-15 06:12:45 +00:00
|
|
|
|
|
2012-04-15 16:42:54 +00:00
|
|
|
|
var card = cards[0];
|
|
|
|
|
|
// each item is an instance of CreditCard
|
|
|
|
|
|
expect(card instanceof CreditCard).toEqual(true);
|
|
|
|
|
|
card.name = "J. Smith";
|
|
|
|
|
|
// non GET methods are mapped onto the instances
|
|
|
|
|
|
card.$save();
|
|
|
|
|
|
// POST: /user/123/card/456 {id:456, number:'1234', name:'J. Smith'}
|
|
|
|
|
|
// server returns: {id:456, number:'1234', name: 'J. Smith'};
|
2011-02-15 06:12:45 +00:00
|
|
|
|
|
2012-04-15 16:42:54 +00:00
|
|
|
|
// our custom method is mapped as well.
|
|
|
|
|
|
card.$charge({amount:9.99});
|
|
|
|
|
|
// POST: /user/123/card/456?amount=9.99&charge=true {id:456, number:'1234', name:'J. Smith'}
|
|
|
|
|
|
});
|
2011-02-15 06:12:45 +00:00
|
|
|
|
|
|
|
|
|
|
// we can create an instance as well
|
|
|
|
|
|
var newCard = new CreditCard({number:'0123'});
|
|
|
|
|
|
newCard.name = "Mike Smith";
|
|
|
|
|
|
newCard.$save();
|
|
|
|
|
|
// POST: /user/123/card {number:'0123', name:'Mike Smith'}
|
|
|
|
|
|
// server returns: {id:789, number:'01234', name: 'Mike Smith'};
|
|
|
|
|
|
expect(newCard.id).toEqual(789);
|
|
|
|
|
|
* </pre>
|
|
|
|
|
|
*
|
|
|
|
|
|
* The object returned from this function execution is a resource "class" which has "static" method
|
|
|
|
|
|
* for each action in the definition.
|
|
|
|
|
|
*
|
2012-05-17 15:11:25 +00:00
|
|
|
|
* Calling these methods invoke `$http` on the `url` template with the given `method`, `params` and `headers`.
|
2011-02-15 06:12:45 +00:00
|
|
|
|
* 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>
|
|
|
|
|
|
var User = $resource('/user/:userId', {userId:'@id'});
|
2011-10-07 18:27:49 +00:00
|
|
|
|
var user = User.get({userId:123}, function() {
|
2011-02-15 06:12:45 +00:00
|
|
|
|
user.abc = true;
|
|
|
|
|
|
user.$save();
|
|
|
|
|
|
});
|
|
|
|
|
|
</pre>
|
|
|
|
|
|
*
|
2013-02-10 19:42:55 +00:00
|
|
|
|
* It's worth noting that the success callback for `get`, `query` and other method gets passed
|
|
|
|
|
|
* in the response that came from the server as well as $http header getter function, so one
|
|
|
|
|
|
* could rewrite the above example and get access to http headers as:
|
2011-02-15 06:12:45 +00:00
|
|
|
|
*
|
|
|
|
|
|
<pre>
|
|
|
|
|
|
var User = $resource('/user/:userId', {userId:'@id'});
|
2011-12-01 21:20:08 +00:00
|
|
|
|
User.get({userId:123}, function(u, getResponseHeaders){
|
2011-02-15 06:12:45 +00:00
|
|
|
|
u.abc = true;
|
2011-12-01 21:20:08 +00:00
|
|
|
|
u.$save(function(u, putResponseHeaders) {
|
|
|
|
|
|
//u => saved user object
|
|
|
|
|
|
//putResponseHeaders => $http header getter
|
|
|
|
|
|
});
|
2011-02-15 06:12:45 +00:00
|
|
|
|
});
|
|
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
|
|
|
|
* # Buzz client
|
|
|
|
|
|
|
|
|
|
|
|
Let's look at what a buzz client created with the `$resource` service looks like:
|
|
|
|
|
|
<doc:example>
|
2011-08-11 03:53:56 +00:00
|
|
|
|
<doc:source jsfiddle="false">
|
2011-02-15 06:12:45 +00:00
|
|
|
|
<script>
|
|
|
|
|
|
function BuzzController($resource) {
|
2011-09-08 20:56:29 +00:00
|
|
|
|
this.userId = 'googlebuzz';
|
2011-02-15 06:12:45 +00:00
|
|
|
|
this.Activity = $resource(
|
|
|
|
|
|
'https://www.googleapis.com/buzz/v1/activities/:userId/:visibility/:activityId/:comments',
|
|
|
|
|
|
{alt:'json', callback:'JSON_CALLBACK'},
|
2011-08-10 13:59:55 +00:00
|
|
|
|
{get:{method:'JSONP', params:{visibility:'@self'}}, replies: {method:'JSONP', params:{visibility:'@self', comments:'@comments'}}}
|
2011-02-15 06:12:45 +00:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BuzzController.prototype = {
|
|
|
|
|
|
fetch: function() {
|
|
|
|
|
|
this.activities = this.Activity.get({userId:this.userId});
|
|
|
|
|
|
},
|
|
|
|
|
|
expandReplies: function(activity) {
|
|
|
|
|
|
activity.replies = this.Activity.replies({userId:this.userId, activityId:activity.id});
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
BuzzController.$inject = ['$resource'];
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
2012-03-09 08:00:05 +00:00
|
|
|
|
<div ng-controller="BuzzController">
|
|
|
|
|
|
<input ng-model="userId"/>
|
|
|
|
|
|
<button ng-click="fetch()">fetch</button>
|
2011-02-15 06:12:45 +00:00
|
|
|
|
<hr/>
|
2012-03-09 08:00:05 +00:00
|
|
|
|
<div ng-repeat="item in activities.data.items">
|
2011-02-15 06:12:45 +00:00
|
|
|
|
<h1 style="font-size: 15px;">
|
|
|
|
|
|
<img src="{{item.actor.thumbnailUrl}}" style="max-height:30px;max-width:30px;"/>
|
|
|
|
|
|
<a href="{{item.actor.profileUrl}}">{{item.actor.name}}</a>
|
2012-03-09 08:00:05 +00:00
|
|
|
|
<a href ng-click="expandReplies(item)" style="float: right;">Expand replies: {{item.links.replies[0].count}}</a>
|
2011-02-15 06:12:45 +00:00
|
|
|
|
</h1>
|
|
|
|
|
|
{{item.object.content | html}}
|
2012-03-09 08:00:05 +00:00
|
|
|
|
<div ng-repeat="reply in item.replies.data.items" style="margin-left: 20px;">
|
2011-02-15 06:12:45 +00:00
|
|
|
|
<img src="{{reply.actor.thumbnailUrl}}" style="max-height:30px;max-width:30px;"/>
|
|
|
|
|
|
<a href="{{reply.actor.profileUrl}}">{{reply.actor.name}}</a>: {{reply.content | html}}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</doc:source>
|
|
|
|
|
|
<doc:scenario>
|
|
|
|
|
|
</doc:scenario>
|
|
|
|
|
|
</doc:example>
|
|
|
|
|
|
*/
|
2012-03-27 04:18:01 +00:00
|
|
|
|
angular.module('ngResource', ['ng']).
|
|
|
|
|
|
factory('$resource', ['$http', '$parse', function($http, $parse) {
|
2012-02-29 01:33:54 +00:00
|
|
|
|
var DEFAULT_ACTIONS = {
|
|
|
|
|
|
'get': {method:'GET'},
|
|
|
|
|
|
'save': {method:'POST'},
|
|
|
|
|
|
'query': {method:'GET', isArray:true},
|
|
|
|
|
|
'remove': {method:'DELETE'},
|
|
|
|
|
|
'delete': {method:'DELETE'}
|
|
|
|
|
|
};
|
2012-03-27 04:45:38 +00:00
|
|
|
|
var noop = angular.noop,
|
|
|
|
|
|
forEach = angular.forEach,
|
2012-03-27 04:18:01 +00:00
|
|
|
|
extend = angular.extend,
|
|
|
|
|
|
copy = angular.copy,
|
|
|
|
|
|
isFunction = angular.isFunction,
|
|
|
|
|
|
getter = function(obj, path) {
|
|
|
|
|
|
return $parse(path)(obj);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2012-11-25 15:10:37 +00:00
|
|
|
|
/**
|
2012-12-07 18:09:42 +00:00
|
|
|
|
* We need our custom method because encodeURIComponent is too aggressive and doesn't follow
|
2012-11-25 15:10:37 +00:00
|
|
|
|
* http://www.ietf.org/rfc/rfc3986.txt with regards to the character set (pchar) allowed in path
|
|
|
|
|
|
* segments:
|
|
|
|
|
|
* segment = *pchar
|
|
|
|
|
|
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
|
|
|
|
|
|
* pct-encoded = "%" HEXDIG HEXDIG
|
|
|
|
|
|
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
|
|
|
|
|
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
|
|
|
|
|
|
* / "*" / "+" / "," / ";" / "="
|
|
|
|
|
|
*/
|
|
|
|
|
|
function encodeUriSegment(val) {
|
|
|
|
|
|
return encodeUriQuery(val, true).
|
|
|
|
|
|
replace(/%26/gi, '&').
|
|
|
|
|
|
replace(/%3D/gi, '=').
|
|
|
|
|
|
replace(/%2B/gi, '+');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* This method is intended for encoding *key* or *value* parts of query component. We need a custom
|
2013-03-21 19:09:47 +00:00
|
|
|
|
* method because encodeURIComponent is too aggressive and encodes stuff that doesn't have to be
|
2012-11-25 15:10:37 +00:00
|
|
|
|
* encoded per http://tools.ietf.org/html/rfc3986:
|
|
|
|
|
|
* query = *( pchar / "/" / "?" )
|
|
|
|
|
|
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
|
|
|
|
|
|
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
|
|
|
|
|
* pct-encoded = "%" HEXDIG HEXDIG
|
|
|
|
|
|
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
|
|
|
|
|
|
* / "*" / "+" / "," / ";" / "="
|
|
|
|
|
|
*/
|
|
|
|
|
|
function encodeUriQuery(val, pctEncodeSpaces) {
|
|
|
|
|
|
return encodeURIComponent(val).
|
|
|
|
|
|
replace(/%40/gi, '@').
|
|
|
|
|
|
replace(/%3A/gi, ':').
|
|
|
|
|
|
replace(/%24/g, '$').
|
|
|
|
|
|
replace(/%2C/gi, ',').
|
2013-02-26 05:25:18 +00:00
|
|
|
|
replace(/%20/g, (pctEncodeSpaces ? '%20' : '+'));
|
2012-11-25 15:10:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function Route(template, defaults) {
|
2013-05-08 22:08:49 +00:00
|
|
|
|
this.template = template;
|
2012-02-29 01:33:54 +00:00
|
|
|
|
this.defaults = defaults || {};
|
2013-02-22 02:49:26 +00:00
|
|
|
|
this.urlParams = {};
|
2012-02-29 01:33:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Route.prototype = {
|
2013-02-22 02:49:26 +00:00
|
|
|
|
setUrlParams: function(config, params, actionUrl) {
|
2012-02-29 01:33:54 +00:00
|
|
|
|
var self = this,
|
2013-02-22 02:49:26 +00:00
|
|
|
|
url = actionUrl || self.template,
|
2012-05-11 20:14:59 +00:00
|
|
|
|
val,
|
2012-02-29 01:33:54 +00:00
|
|
|
|
encodedVal;
|
|
|
|
|
|
|
2013-02-22 02:49:26 +00:00
|
|
|
|
var urlParams = self.urlParams = {};
|
|
|
|
|
|
forEach(url.split(/\W/), function(param){
|
|
|
|
|
|
if (param && (new RegExp("(^|[^\\\\]):" + param + "(\\W|$)").test(url))) {
|
|
|
|
|
|
urlParams[param] = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
url = url.replace(/\\:/g, ':');
|
|
|
|
|
|
|
2012-02-29 01:33:54 +00:00
|
|
|
|
params = params || {};
|
2013-02-22 02:49:26 +00:00
|
|
|
|
forEach(self.urlParams, function(_, urlParam){
|
2012-08-20 22:47:26 +00:00
|
|
|
|
val = params.hasOwnProperty(urlParam) ? params[urlParam] : self.defaults[urlParam];
|
2012-09-10 20:40:32 +00:00
|
|
|
|
if (angular.isDefined(val) && val !== null) {
|
2012-05-11 20:14:59 +00:00
|
|
|
|
encodedVal = encodeUriSegment(val);
|
2013-02-22 02:49:26 +00:00
|
|
|
|
url = url.replace(new RegExp(":" + urlParam + "(\\W|$)", "g"), encodedVal + "$1");
|
2012-05-11 20:14:59 +00:00
|
|
|
|
} else {
|
2013-02-22 02:49:26 +00:00
|
|
|
|
url = url.replace(new RegExp("(\/?):" + urlParam + "(\\W|$)", "g"), function(match,
|
2013-01-08 22:19:40 +00:00
|
|
|
|
leadingSlashes, tail) {
|
|
|
|
|
|
if (tail.charAt(0) == '/') {
|
|
|
|
|
|
return tail;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return leadingSlashes + tail;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2012-05-11 20:14:59 +00:00
|
|
|
|
}
|
2012-02-29 01:33:54 +00:00
|
|
|
|
});
|
2013-01-30 15:41:39 +00:00
|
|
|
|
|
2013-05-08 22:08:49 +00:00
|
|
|
|
// strip trailing slashes and set the url
|
|
|
|
|
|
config.url = url.replace(/\/+$/, '');
|
2013-01-30 15:41:39 +00:00
|
|
|
|
|
|
|
|
|
|
// set params - delegate param encoding to $http
|
2012-03-27 04:18:01 +00:00
|
|
|
|
forEach(params, function(value, key){
|
2012-02-29 01:33:54 +00:00
|
|
|
|
if (!self.urlParams[key]) {
|
2013-01-30 15:41:39 +00:00
|
|
|
|
config.params = config.params || {};
|
|
|
|
|
|
config.params[key] = value;
|
2012-02-29 01:33:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function ResourceFactory(url, paramDefaults, actions) {
|
|
|
|
|
|
var route = new Route(url);
|
|
|
|
|
|
|
|
|
|
|
|
actions = extend({}, DEFAULT_ACTIONS, actions);
|
|
|
|
|
|
|
2012-05-11 20:14:59 +00:00
|
|
|
|
function extractParams(data, actionParams){
|
2012-02-29 01:33:54 +00:00
|
|
|
|
var ids = {};
|
2012-09-22 23:53:08 +00:00
|
|
|
|
actionParams = extend({}, paramDefaults, actionParams);
|
|
|
|
|
|
forEach(actionParams, function(value, key){
|
2012-07-04 08:07:09 +00:00
|
|
|
|
if (isFunction(value)) { value = value(); }
|
2012-02-29 01:33:54 +00:00
|
|
|
|
ids[key] = value.charAt && value.charAt(0) == '@' ? getter(data, value.substr(1)) : value;
|
|
|
|
|
|
});
|
|
|
|
|
|
return ids;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function Resource(value){
|
|
|
|
|
|
copy(value || {}, this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
forEach(actions, function(action, name) {
|
2012-11-27 06:24:35 +00:00
|
|
|
|
action.method = angular.uppercase(action.method);
|
2012-04-20 08:31:25 +00:00
|
|
|
|
var hasBody = action.method == 'POST' || action.method == 'PUT' || action.method == 'PATCH';
|
2012-02-29 01:33:54 +00:00
|
|
|
|
Resource[name] = function(a1, a2, a3, a4) {
|
|
|
|
|
|
var params = {};
|
|
|
|
|
|
var data;
|
|
|
|
|
|
var success = noop;
|
|
|
|
|
|
var error = null;
|
2013-01-17 22:21:35 +00:00
|
|
|
|
var promise;
|
|
|
|
|
|
|
2012-02-29 01:33:54 +00:00
|
|
|
|
switch(arguments.length) {
|
|
|
|
|
|
case 4:
|
|
|
|
|
|
error = a4;
|
|
|
|
|
|
success = a3;
|
|
|
|
|
|
//fallthrough
|
|
|
|
|
|
case 3:
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
if (isFunction(a2)) {
|
|
|
|
|
|
if (isFunction(a1)) {
|
|
|
|
|
|
success = a1;
|
|
|
|
|
|
error = a2;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
success = a2;
|
|
|
|
|
|
error = a3;
|
|
|
|
|
|
//fallthrough
|
|
|
|
|
|
} else {
|
|
|
|
|
|
params = a1;
|
|
|
|
|
|
data = a2;
|
|
|
|
|
|
success = a3;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
if (isFunction(a1)) success = a1;
|
2012-04-20 08:31:25 +00:00
|
|
|
|
else if (hasBody) data = a1;
|
2012-02-29 01:33:54 +00:00
|
|
|
|
else params = a1;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 0: break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
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));
|
2013-01-17 22:21:35 +00:00
|
|
|
|
var httpConfig = {},
|
|
|
|
|
|
promise;
|
2013-01-18 06:01:50 +00:00
|
|
|
|
|
|
|
|
|
|
forEach(action, function(value, key) {
|
|
|
|
|
|
if (key != 'params' && key != 'isArray' ) {
|
|
|
|
|
|
httpConfig[key] = copy(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
httpConfig.data = data;
|
2013-02-22 02:49:26 +00:00
|
|
|
|
route.setUrlParams(httpConfig, extend({}, extractParams(data, action.params || {}), params), action.url);
|
2013-01-18 06:01:50 +00:00
|
|
|
|
|
2013-02-12 05:43:30 +00:00
|
|
|
|
function markResolved() { value.$resolved = true; }
|
2012-02-29 01:33:54 +00:00
|
|
|
|
|
2013-01-17 22:21:35 +00:00
|
|
|
|
promise = $http(httpConfig);
|
|
|
|
|
|
value.$resolved = false;
|
2013-02-12 05:43:30 +00:00
|
|
|
|
|
|
|
|
|
|
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;
|
2012-02-29 01:33:54 +00:00
|
|
|
|
}
|
2013-02-12 05:43:30 +00:00
|
|
|
|
}
|
2012-02-29 01:33:54 +00:00
|
|
|
|
|
2013-02-12 05:43:30 +00:00
|
|
|
|
(success||noop)(value, response.headers);
|
2013-01-17 22:21:35 +00:00
|
|
|
|
|
2013-02-12 05:43:30 +00:00
|
|
|
|
response.resource = value;
|
|
|
|
|
|
return response;
|
|
|
|
|
|
}, error).then;
|
|
|
|
|
|
|
|
|
|
|
|
return value;
|
2012-02-29 01:33:54 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Resource.prototype['$' + name] = function(a1, a2, a3) {
|
|
|
|
|
|
var params = extractParams(this),
|
|
|
|
|
|
success = noop,
|
|
|
|
|
|
error;
|
|
|
|
|
|
|
|
|
|
|
|
switch(arguments.length) {
|
|
|
|
|
|
case 3: params = a1; success = a2; error = a3; break;
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
if (isFunction(a1)) {
|
|
|
|
|
|
success = a1;
|
|
|
|
|
|
error = a2;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
params = a1;
|
|
|
|
|
|
success = a2 || noop;
|
|
|
|
|
|
}
|
|
|
|
|
|
case 0: break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw "Expected between 1-3 arguments [params, success, error], got " +
|
|
|
|
|
|
arguments.length + " arguments.";
|
|
|
|
|
|
}
|
2012-04-20 08:31:25 +00:00
|
|
|
|
var data = hasBody ? this : undefined;
|
2012-02-29 01:33:54 +00:00
|
|
|
|
Resource[name].call(this, params, data, success, error);
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
2012-11-25 15:10:37 +00:00
|
|
|
|
|
|
|
|
|
|
Resource.bind = function(additionalParamDefaults){
|
|
|
|
|
|
return ResourceFactory(url, extend({}, paramDefaults, additionalParamDefaults), actions);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2012-02-29 01:33:54 +00:00
|
|
|
|
return Resource;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ResourceFactory;
|
2012-03-27 04:18:01 +00:00
|
|
|
|
}]);
|