Files

8 lines
6.7 KiB
JavaScript

/**
* Restfull Resources service for AngularJS apps
* @version v0.7.3 - 2013-05-31
* @link https://github.com/mgonto/restangular
* @author Martin Gontovnikas <martin@gonto.com.ar>
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
(function(){var t=angular.module("restangular",["ngResource"]);t.provider("Restangular",function(){function t(t){return _.contains(i,t)}function e(t){return _.defaults(t,s)}function r(t,e,r){var n=R[r],i=t;return n&&_.each(n,function(t){i=t(e,i)}),b(i,e,r)}var n=this,i=["get","head","options","trace"],o="";this.setBaseUrl=function(t){o=t};var a=[];this.setExtraFields=function(t){a=t};var s={};this.setDefaultHttpFields=function(t){s=t};var u={};this.setDefaultRequestParams=function(t){u=t};var c=[];this.setMethodOverriders=function(t){var e=_.extend([],t);d("delete",e)&&e.push("remove"),c=e};var d=function(t,e){var r=e||c;return!_.isUndefined(_.find(r,function(e){return e.toLowerCase()===t.toLowerCase()}))},h="path";this.setUrlCreator=function(t){if(!_.has(T,t))throw Error("URL Path selected isn't valid");h=t};var l={id:"id",route:"route",parentResource:"parentResource",restangularCollection:"restangularCollection",what:"restangularWhat"};this.setRestangularFields=function(t){l=_.extend(l,t)};var f=function(t,e){var r=l.id.split("."),n=t;_.each(_.initial(r),function(t){n[t]={},n=n[t]}),n[_.last(r)]=e},p=function(t){var e=l.id.split("."),r=angular.copy(t);return _.each(e,function(t){r=r[t]}),r},v=function(t){return t};this.setResponseExtractor=function(t){v=t},this.setResponseInterceptor=this.setResponseExtractor;var m=function(t){return t};this.setRequestInterceptor=function(t){m=t};var b=function(t){return t};this.setOnElemRestangularized=function(t){b=t};var g=!0;this.setListTypeIsArray=function(t){g=t};var y=null;this.setRequestSuffix=function(t){y=t};var R={};this.addElementTransformer=function(t,e,r){var n=null,i=null;2===arguments.length?i=e:(i=r,n=e);var o=R[t];o||(o=R[t]=[]),o.push(function(t,e){return _.isNull(n)||t==n?i(e):e})};var T={},C=function(){};C.prototype.parentsArray=function(t){for(var e=[];!_.isUndefined(t);)e.push(t),t=t[l.parentResource];return e.reverse()},C.prototype.resource=function(t,r,n,i){var o=this.base(t);return o+=i[l.what]?"/:"+l.what:"",o+=y||"",r(o,{},{getList:e({method:"GET",params:u,isArray:g,headers:n||{}}),get:e({method:"GET",params:u,isArray:!1,headers:n||{}}),put:e({method:"PUT",params:u,isArray:!1,headers:n||{}}),post:e({method:"POST",params:u,isArray:!1,headers:n||{}}),remove:e({method:"DELETE",params:u,isArray:!1,headers:n||{}}),head:e({method:"HEAD",params:u,isArray:!1,headers:n||{}}),trace:e({method:"TRACE",params:u,isArray:!1,headers:n||{}}),options:e({method:"OPTIONS",params:u,isArray:!1,headers:n||{}}),patch:e({method:"PATCH",params:u,isArray:!1,headers:n||{}})})};var E=function(){};E.prototype=new C,E.prototype.base=function(t){return o+_.reduce(this.parentsArray(t),function(t,e){var r=t+"/"+e[l.route];return e[l.restangularCollection]||(r+="/"+p(e)),r},"")},E.prototype.fetchUrl=function(t,e){var r=this.base(t);return e&&e[l.what]&&(r+="/"+e[l.what]),r},T.path=E,this.$get=["$resource","$q",function(e,i){function o(t,e,r){if(e[l.route]=r,e.getRestangularUrl=_.bind(j.fetchUrl,j,e),e.addRestangularMethod=_.bind($,e),t){var n=_.union(_.values(_.pick(l,["id","route","parentResource"])),a);e[l.parentResource]=_.pick(t,n)}return e}function s(t,e,r){var n={};return f(n,r),C(t,n,e)}function u(t,e){return E(t,{},e,!0)}function c(t,e){return t.call=_.bind(p,t),t.get=_.bind(b,t),t[l.restangularCollection]=e,e&&(t.push=_.bind(p,t,"push")),t}function p(t){var e=i.defer(),r=arguments;return this.then(function(n){var i=Array.prototype.slice.call(r,1),o=n[t];o.apply(n,i),e.resolve(n)}),c(e.promise,this[l.restangularCollection])}function b(t){var e=i.defer();return this.then(function(r){e.resolve(r[t])}),c(e.promise,this[l.restangularCollection])}function g(t){return _.omit(t,_.values(_.omit(l,"id")))}function y(t){t.customOperation=_.bind(H,t),_.each(["put","post","get","delete"],function(e){_.each(["do","custom"],function(r){var n=r+e.toUpperCase();t[n]=_.bind(H,t,e)})}),t.customGETLIST=_.bind(w,t),t.doGETLIST=t.customGETLIST}function R(t){var e=angular.copy(t);return C(e[l.parentResource],e,e[l.route])}function C(t,e,n){var i=o(t,e,n);return i[l.restangularCollection]=!1,i.get=_.bind(U,i),i.getList=_.bind(w,i),i.put=_.bind(O,i),i.post=_.bind(P,i),i.remove=_.bind(I,i),i.head=_.bind(S,i),i.trace=_.bind(G,i),i.options=_.bind(q,i),i.patch=_.bind(D,i),i.one=_.bind(s,i,i),i.all=_.bind(u,i,i),y(i),r(i,!1,n)}function E(t,e,n){var i=o(t,e,n);return i[l.restangularCollection]=!0,i.post=_.bind(P,i,null),i.head=_.bind(S,i),i.trace=_.bind(G,i),i.putElement=_.bind(L,i),i.options=_.bind(q,i),i.patch=_.bind(D,i),i.getList=_.bind(w,i,null),y(i),r(i,!0,n)}function A(t){var e={};return t&&(e[l.what]=t),e}function L(t,e,r){var n=this,o=this[t],a=i.defer();return o.put(e,r).then(function(e){var r=R(n);r[t]=e,a.resolve(r)},function(t){a.reject(t)}),c(a.promise,!0)}function w(t,r,n){var o=A(t),a=this,s=i.defer(),u="getList",d=j.fetchUrl(this,o),h=t||a[l.route],f=_.extend(o,r);return m(null,u,h,d),j.resource(this,e,n,f).getList(f,function(e){var r=v(e,u,h,d),n=_.map(r,function(e){return a[l.restangularCollection]?C(null,e,a[l.route]):C(a,e,t)});n=_.extend(r,n),a[l.restangularCollection]?s.resolve(E(null,n,a[l.route])):s.resolve(E(a,n,t))},function(t){s.reject(t)}),c(s.promise,!0)}function x(r,n,o,a){var s=this,u=i.defer(),h=n||{},f=o||this,p=h[l.what]||this[l.route],b=j.fetchUrl(this,h),y=o||g(this);y=m(y,r,p,b);var R=function(t){var e=v(t,r,p,b)||f;"post"!==r||s[l.restangularCollection]?u.resolve(C(s[l.parentResource],e,s[l.route])):u.resolve(C(s,e,h[l.what]))},T=function(t){u.reject(t)},E=r,A=_.extend({},a),L=d(r);return L&&(E="post",A=_.extend(A,{"X-HTTP-Method-Override":r})),t(r)?L?j.resource(this,e,A,h)[E](h,{},R,T):j.resource(this,e,A,h)[E](h,R,T):j.resource(this,e,A,h)[E](h,y,R,T),c(u.promise)}function U(t,e){return _.bind(x,this)("get",t,void 0,e)}function I(t,e){return _.bind(x,this)("remove",t,void 0,e)}function O(t,e){return _.bind(x,this)("put",t,void 0,e)}function P(t,e,r,n){return _.bind(x,this)("post",_.extend(A(t),r),e,n)}function S(t,e){return _.bind(x,this)("head",t,void 0,e)}function G(t,e){return _.bind(x,this)("trace",t,void 0,e)}function q(t,e){return _.bind(x,this)("options",t,void 0,e)}function D(t,e){return _.bind(x,this)("patch",t,void 0,e)}function H(t,e,r,n,i){return _.bind(x,this)(t,_.extend(A(e),r),i,n)}function $(t,e,r,n,i,o){var a;a="getList"===e?_.bind(w,this,r):_.bind(H,this,e,r),this[t]=function(t,e,r){var s=_.defaults({params:t,headers:e,elem:r},{params:n,headers:i,elem:o});return a(s.params,s.headers,s.elem)}}var j=new T[h],F={};return _.extend(F,_.omit(n,"$get")),F.copy=_.bind(R,F),F.one=_.bind(s,F,null),F.all=_.bind(u,F,null),F}]})})();