mirror of
https://github.com/wahyd4/cdnjs.git
synced 2026-08-14 15:25:52 +10:00
8 lines
2.6 KiB
JavaScript
8 lines
2.6 KiB
JavaScript
/**
|
|
* Restfull Resources service for AngularJS apps
|
|
* @version v0.3.2 - 2013-04-12
|
|
* @link https://github.com/mgonto/restangular
|
|
* @author Martin Gontovnikas <martin@gonto.com.ar>
|
|
* @license MIT License, http://www.opensource.org/licenses/MIT
|
|
*/
|
|
var module=angular.module("restangular",["ngResource"]);module.provider("Restangular",function(){this.baseUrl="",this.setBaseUrl=function(r){this.baseUrl=r},this.extraFields=[],this.setExtraFields=function(r){this.extraFields=r},this.urlCreator="path",this.setUrlCreator=function(e){if(!_.has(r,e))throw Error("URL Path selected isn't valid");this.urlCreator=e},this.responseExtractor=function(r){return r},this.setResponseExtractor=function(r){this.responseExtractor=r};var r={},e=function(r){this.baseUrl=r};e.prototype.base=function(r){return this.baseUrl+_.reduce(this.parentsArray(r),function(r,e){var t=r+"/"+e.route;return e.id&&(t+="/"+e.id),t},"")},e.prototype.parentsArray=function(r){for(var e=[];!_.isUndefined(r);)e.push(r),r=r.parentResource;return e.reverse()},e.prototype.fetchUrl=function(r,e){return this.base(e)+"/"+r.toLowerCase()},e.prototype.resource=function(r,e,t){return e(this.base(r)+"/:what",{},{getArray:{method:"GET",params:{},isArray:!0,headers:t||{}},get:{method:"GET",params:{},isArray:!1,headers:t||{}},put:{method:"PUT",params:{},isArray:!1,headers:t||{}},post:{method:"POST",params:{},isArray:!1,headers:t||{}},remove:{method:"DELETE",params:{},isArray:!1,headers:t||{}}})},r.path=e,this.$get=["$resource","$q",function(e,t){function n(r,e,t){return e.route=t,e.getList=_.bind(s,e),e.get=_.bind(o,e),e.put=_.bind(u,e),e.post=_.bind(h,e),e.remove=_.bind(a,e),r&&(e.parentResource=_.pick(r,_.union(["id","route","parentResource"],d))),e}function s(r,s,i){var o=r?{what:r}:{},a=this,u=t.defer();return c.resource(this,e,i).getArray(_.extend(o,s),function(e){var t=p(e,"get"),s=_.map(t,function(e){return r?n(a,e,r):n(null,e,a.route)});u.resolve(s)},function(){u.reject(arguments)}),u.promise}function i(r,s,i,o){var a=this,u=t.defer(),h=s||{},d=i||this,l=function(e){var t=p(e,r);t?u.resolve(n(a.parentResource,t,a.route)):u.resolve()},f=function(){u.reject(arguments)};return"get"===r?c.resource(this,e,o)[r](h,l,f):c.resource(this,e,o)[r](h,d,l,f),u.promise}function o(r,e){return _.bind(i,this)("get",r,void 0,e)}function a(r,e){return _.bind(i,this)("remove",r,{},e)}function u(r,e){return _.bind(i,this)("put",r,void 0,e)}function h(r,e,t,n){return _.bind(i,this)("post",_.extend({what:r},t),e,n)}var c=new r[this.urlCreator](this.baseUrl),d=this.extraFields,p=this.responseExtractor,l={};return l.one=function(r,e){return n(null,{id:e},r)},l.all=function(r){return n(null,{},r)},l}]}); |