mirror of
https://github.com/wahyd4/cdnjs.git
synced 2026-08-15 15:56:28 +10:00
8 lines
4.2 KiB
JavaScript
8 lines
4.2 KiB
JavaScript
/**
|
|
* Restfull Resources service for AngularJS apps
|
|
* @version v0.5.2 - 2013-04-23
|
|
* @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(){function t(t){return _.contains(e,t)}var e=["get","head","options","trace"],r="";this.setBaseUrl=function(t){r=t};var n=[];this.setExtraFields=function(t){n=t};var i="path";this.setUrlCreator=function(t){if(!_.has(c,t))throw Error("URL Path selected isn't valid");i=t};var o={id:"id",route:"route",parentResource:"parentResource",restangularCollection:"restangularCollection",what:"restangularWhat"};this.setRestangularFields=function(t){o=_.extend(o,t)};var a=function(t){return t};this.setResponseExtractor=function(t){a=t},this.setResponseInterceptor=this.setResponseExtractor;var s=!0;this.setListTypeIsArray=function(t){s=t};var u=null;this.setRequestSuffix=function(t){u=t};var c={},d=function(){};d.prototype.base=function(t){return r+_.reduce(this.parentsArray(t),function(t,e){var r=t+"/"+e[o.route];return e[o.restangularCollection]||(r+="/"+e[o.id]),r},"")},d.prototype.parentsArray=function(t){for(var e=[];!_.isUndefined(t);)e.push(t),t=t[o.parentResource];return e.reverse()},d.prototype.fetchUrl=function(t,e){var r=this.base(t);return e[o.what]&&(r+="/"+e[o.what]),r},d.prototype.resource=function(t,e,r){var n=u?{restangularSuffix:u}:{};return e(this.base(t)+"/:"+o.what+":restangularSuffix",{},{getList:{method:"GET",params:n,isArray:s,headers:r||{}},get:{method:"GET",params:n,isArray:!1,headers:r||{}},put:{method:"PUT",params:n,isArray:!1,headers:r||{}},post:{method:"POST",params:n,isArray:!1,headers:r||{}},remove:{method:"DELETE",params:n,isArray:!1,headers:r||{}},head:{method:"HEAD",params:n,isArray:!1,headers:r||{}},trace:{method:"TRACE",params:n,isArray:!1,headers:r||{}},options:{method:"OPTIONS",params:n,isArray:!1,headers:r||{}},patch:{method:"PATCH",params:n,isArray:!1,headers:r||{}}})},c.path=d,this.$get=["$resource","$q",function(e,r){function s(t,e,r){if(e[o.route]=r,t){var i=_.chain(o).pick(["id","route","parentResource"]).values().union(n).value();e[o.parentResource]=_.pick(t,i)}return e}function u(t){t.customOperation=_.bind(E,t),_.each(["put","post","get","delete"],function(e){var r="custom"+e.toUpperCase();t[r]=_.bind(E,t,e)}),t.customGETLIST=_.bind(p,t)}function d(t,e,r){var n=s(t,e,r);return n[o.restangularCollection]=!1,n.get=_.bind(v,n),n.getList=_.bind(p,n),n.put=_.bind(m,n),n.post=_.bind(g,n),n.remove=_.bind(b,n),n.head=_.bind(y,n),n.trace=_.bind(A,n),n.options=_.bind(R,n),n.patch=_.bind(C,n),u(e),n}function h(t,e,r){var n=s(t,e,r);return n[o.restangularCollection]=!0,n.post=_.bind(g,n,null),n.head=_.bind(y,n),n.trace=_.bind(A,n),n.options=_.bind(R,n),n.patch=_.bind(C,n),n.getList=_.bind(p,n,null),u(e),n}function l(t){var e={};return t&&(e[o.what]=t),e}function p(t,n,i){var s=l(t),u=this,c=r.defer();return T.resource(this,e,i).getList(_.extend(s,n),function(e){var r=a(e,"getList",t,T.fetchUrl(u,s)),n=_.map(r,function(e){return u[o.restangularCollection]?d(null,e,u[o.route]):d(u,e,t)});u[o.restangularCollection]?c.resolve(h(null,n,u[o.route])):c.resolve(h(u,n,t))},function(t){c.reject(t)}),c.promise}function f(n,i,s,u){var c=this,h=r.defer(),l=i||{},p=s||this,f=function(t){var e=a(t,n,l[o.what]||c[o.route],T.fetchUrl(c,l))||p;"post"!==n||c[o.restangularCollection]?h.resolve(d(c[o.parentResource],e,c[o.route])):h.resolve(d(c,e,l[o.what]))},v=function(t){h.reject(t)};return t(n)?T.resource(this,e,u)[n](l,f,v):T.resource(this,e,u)[n](l,p,f,v),h.promise}function v(t,e){return _.bind(f,this)("get",t,void 0,e)}function b(t,e){return _.bind(f,this)("remove",t,{},e)}function m(t,e){return _.bind(f,this)("put",t,void 0,e)}function g(t,e,r,n){return _.bind(f,this)("post",_.extend(l(t),r),e,n)}function y(t,e){return _.bind(f,this)("head",t,void 0,e)}function A(t,e){return _.bind(f,this)("trace",t,void 0,e)}function R(t,e){return _.bind(f,this)("options",t,void 0,e)}function C(t,e){return _.bind(f,this)("patch",t,void 0,e)}function E(t,e,r,n,i){return _.bind(f,this)(t,_.extend(l(e),r),i,n)}var T=new c[i],x={};return x.one=function(t,e){var r={};return r[o.id]=e,d(null,r,t)},x.all=function(t){return h(null,{},t,!0)},x}]}); |