mirror of
https://github.com/wahyd4/cdnjs.git
synced 2026-08-17 08:46:19 +10:00
8 lines
4.4 KiB
JavaScript
8 lines
4.4 KiB
JavaScript
/**
|
|
* Restfull Resources service for AngularJS apps
|
|
* @version v0.5.3 - 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,e,r){var n={};return n[o.id]=r,l(t,n,e)}function d(t,e){return p(t,{},e,!0)}function h(t){t.customOperation=_.bind(L,t),_.each(["put","post","get","delete"],function(e){_.each(["do","custom"],function(r){var n=r+e.toUpperCase();t[n]=_.bind(L,t,e)})}),t.customGETLIST=_.bind(v,t),t.doGETLIST=t.customGETLIST}function l(t,e,r){var n=s(t,e,r);return n[o.restangularCollection]=!1,n.get=_.bind(m,n),n.getList=_.bind(v,n),n.put=_.bind(y,n),n.post=_.bind(A,n),n.remove=_.bind(g,n),n.head=_.bind(R,n),n.trace=_.bind(T,n),n.options=_.bind(E,n),n.patch=_.bind(C,n),n.one=_.bind(u,n,n),n.all=_.bind(d,n,n),h(e),n}function p(t,e,r){var n=s(t,e,r);return n[o.restangularCollection]=!0,n.post=_.bind(A,n,null),n.head=_.bind(R,n),n.trace=_.bind(T,n),n.options=_.bind(E,n),n.patch=_.bind(C,n),n.getList=_.bind(v,n,null),h(e),n}function f(t){var e={};return t&&(e[o.what]=t),e}function v(t,n,i){var s=f(t),u=this,c=r.defer();return x.resource(this,e,i).getList(_.extend(s,n),function(e){var r=a(e,"getList",t,x.fetchUrl(u,s)),n=_.map(r,function(e){return u[o.restangularCollection]?l(null,e,u[o.route]):l(u,e,t)});u[o.restangularCollection]?c.resolve(p(null,n,u[o.route])):c.resolve(p(u,n,t))},function(t){c.reject(t)}),c.promise}function b(n,i,s,u){var c=this,d=r.defer(),h=i||{},p=s||this,f=function(t){var e=a(t,n,h[o.what]||c[o.route],x.fetchUrl(c,h))||p;"post"!==n||c[o.restangularCollection]?d.resolve(l(c[o.parentResource],e,c[o.route])):d.resolve(l(c,e,h[o.what]))},_=function(t){d.reject(t)};return t(n)?x.resource(this,e,u)[n](h,f,_):x.resource(this,e,u)[n](h,p,f,_),d.promise}function m(t,e){return _.bind(b,this)("get",t,void 0,e)}function g(t,e){return _.bind(b,this)("remove",t,{},e)}function y(t,e){return _.bind(b,this)("put",t,void 0,e)}function A(t,e,r,n){return _.bind(b,this)("post",_.extend(f(t),r),e,n)}function R(t,e){return _.bind(b,this)("head",t,void 0,e)}function T(t,e){return _.bind(b,this)("trace",t,void 0,e)}function E(t,e){return _.bind(b,this)("options",t,void 0,e)}function C(t,e){return _.bind(b,this)("patch",t,void 0,e)}function L(t,e,r,n,i){return _.bind(b,this)(t,_.extend(f(e),r),i,n)}var x=new c[i],w={};return w.one=_.bind(u,w,null),w.all=_.bind(d,w,null),w}]}); |