Files

8 lines
6.4 KiB
JavaScript

/**
* Restfull Resources service for AngularJS apps
* @version v0.7.0 - 2013-05-28
* @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(n,t)}function e(t){return _.defaults(t,s)}function r(t,e,r){var n=m[r],i=t;return n&&_.each(n,function(t){i=t(e,i)}),f(i,e,r)}var n=["get","head","options","trace"],i="";this.setBaseUrl=function(t){i=t};var o=[];this.setExtraFields=function(t){o=t};var s={};this.setDefaultHttpFields=function(t){s=t};var a=[];this.setMethodOverriders=function(t){var e=_.extend([],t);u("delete",e)&&e.push("remove"),a=e};var u=function(t,e){var r=e||a;return!_.isUndefined(_.find(r,function(e){return e.toLowerCase()===t.toLowerCase()}))},c="path";this.setUrlCreator=function(t){if(!_.has(b,t))throw Error("URL Path selected isn't valid");c=t};var d={id:"id",route:"route",parentResource:"parentResource",restangularCollection:"restangularCollection",what:"restangularWhat"};this.setRestangularFields=function(t){d=_.extend(d,t)};var h=function(t){return t};this.setResponseExtractor=function(t){h=t},this.setResponseInterceptor=this.setResponseExtractor;var l=function(t){return t};this.setRequestInterceptor=function(t){l=t};var f=function(t){return t};this.setOnElemRestangularized=function(t){f=t};var p=!0;this.setListTypeIsArray=function(t){p=t};var v=null;this.setRequestSuffix=function(t){v=t};var m={};this.addElementTransformer=function(t,e,r){var n=null,i=null;2===arguments.length?i=e:(i=r,n=e);var o=m[t];o||(o=m[t]=[]),o.push(function(t,e){return _.isNull(n)||t==n?i(e):e})};var b={},g=function(){};g.prototype.parentsArray=function(t){for(var e=[];!_.isUndefined(t);)e.push(t),t=t[d.parentResource];return e.reverse()},g.prototype.resource=function(t,r,n,i){var o={},s=this.base(t);return s+=i[d.what]?"/:"+d.what:"",s+=v||"",r(s,{},{getList:e({method:"GET",params:o,isArray:p,headers:n||{}}),get:e({method:"GET",params:o,isArray:!1,headers:n||{}}),put:e({method:"PUT",params:o,isArray:!1,headers:n||{}}),post:e({method:"POST",params:o,isArray:!1,headers:n||{}}),remove:e({method:"DELETE",params:o,isArray:!1,headers:n||{}}),head:e({method:"HEAD",params:o,isArray:!1,headers:n||{}}),trace:e({method:"TRACE",params:o,isArray:!1,headers:n||{}}),options:e({method:"OPTIONS",params:o,isArray:!1,headers:n||{}}),patch:e({method:"PATCH",params:o,isArray:!1,headers:n||{}})})};var y=function(){};y.prototype=new g,y.prototype.base=function(t){return i+_.reduce(this.parentsArray(t),function(t,e){var r=t+"/"+e[d.route];return e[d.restangularCollection]||(r+="/"+e[d.id]),r},"")},y.prototype.fetchUrl=function(t,e){var r=this.base(t);return e&&e[d.what]&&(r+="/"+e[d.what]),r},b.path=y,this.$get=["$resource","$q",function(e,n){function i(t,e,r){if(e[d.route]=r,e.url=_.bind(q.fetchUrl,q,e),e.addRestangularMethod=_.bind(j,e),t){var n=_.union(_.values(_.pick(d,["id","route","parentResource"])),o);e[d.parentResource]=_.pick(t,n)}return e}function s(t,e,r){var n={};return n[d.id]=r,R(t,n,e)}function a(t,e){return T(t,{},e,!0)}function f(t,e){return t.call=_.bind(p,t),t.get=_.bind(v,t),t[d.restangularCollection]=e,e&&(t.push=_.bind(p,t,"push")),t}function p(t){var e=n.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)}),f(e.promise,this[d.restangularCollection])}function v(t){var e=n.defer();return this.then(function(r){e.resolve(r[t])}),f(e.promise,this[d.restangularCollection])}function m(t){return _.omit(t,_.values(_.omit(d,"id")))}function g(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(A,t),t.doGETLIST=t.customGETLIST}function y(t){var e=angular.copy(t);return R(e[d.parentResource],e,e[d.route])}function R(t,e,n){var o=i(t,e,n);return o[d.restangularCollection]=!1,o.get=_.bind(w,o),o.getList=_.bind(A,o),o.put=_.bind(U,o),o.post=_.bind(I,o),o.remove=_.bind(x,o),o.head=_.bind(O,o),o.trace=_.bind(P,o),o.options=_.bind(S,o),o.patch=_.bind(G,o),o.one=_.bind(s,o,o),o.all=_.bind(a,o,o),g(o),r(o,!1,n)}function T(t,e,n){var o=i(t,e,n);return o[d.restangularCollection]=!0,o.post=_.bind(I,o,null),o.head=_.bind(O,o),o.trace=_.bind(P,o),o.putElement=_.bind(E,o),o.options=_.bind(S,o),o.patch=_.bind(G,o),o.getList=_.bind(A,o,null),g(o),r(o,!0,n)}function C(t){var e={};return t&&(e[d.what]=t),e}function E(t,e,r){var i=this,o=this[t],s=n.defer();return o.put(e,r).then(function(e){var r=y(i);r[t]=e,s.resolve(r)},function(t){s.reject(t)}),f(s.promise,!0)}function A(t,r,i){var o=C(t),s=this,a=n.defer(),u="getList",c=q.fetchUrl(this,o),p=t||s[d.route],v=_.extend(o,r);return l(null,u,p,c),q.resource(this,e,i,v).getList(v,function(e){var r=h(e,u,p,c),n=_.map(r,function(e){return s[d.restangularCollection]?R(null,e,s[d.route]):R(s,e,t)});n=_.extend(r,n),s[d.restangularCollection]?a.resolve(T(null,n,s[d.route])):a.resolve(T(s,n,t))},function(t){a.reject(t)}),f(a.promise,!0)}function L(r,i,o,s){var a=this,c=n.defer(),p=i||{},v=o||this,b=p[d.what]||this[d.route],g=q.fetchUrl(this,p),y=o||m(this);y=l(y,r,b,g);var T=function(t){var e=h(t,r,b,g)||v;"post"!==r||a[d.restangularCollection]?c.resolve(R(a[d.parentResource],e,a[d.route])):c.resolve(R(a,e,p[d.what]))},C=function(t){c.reject(t)},E=r,A=_.extend({},s),L=u(r);return L&&(E="post",A=_.extend(A,{"X-HTTP-Method-Override":r})),t(r)?L?q.resource(this,e,A,p)[E](p,{},T,C):q.resource(this,e,A,p)[E](p,T,C):q.resource(this,e,A,p)[E](p,y,T,C),f(c.promise)}function w(t,e){return _.bind(L,this)("get",t,void 0,e)}function x(t,e){return _.bind(L,this)("remove",t,{},e)}function U(t,e){return _.bind(L,this)("put",t,void 0,e)}function I(t,e,r,n){return _.bind(L,this)("post",_.extend(C(t),r),e,n)}function O(t,e){return _.bind(L,this)("head",t,void 0,e)}function P(t,e){return _.bind(L,this)("trace",t,void 0,e)}function S(t,e){return _.bind(L,this)("options",t,void 0,e)}function G(t,e){return _.bind(L,this)("patch",t,void 0,e)}function H(t,e,r,n,i){return _.bind(L,this)(t,_.extend(C(e),r),i,n)}function j(t,e,r,n,i,o){var s;s="getList"===e?_.bind(A,this,r):_.bind(H,this,e,r),this[t]=function(t,e,r){var a=_.defaults({params:t,headers:e,elem:r},{params:n,headers:i,elem:o});return s(a.params,a.headers,a.elem)}}var q=new b[c],D={};return D.copy=_.bind(y,D),D.one=_.bind(s,D,null),D.all=_.bind(a,D,null),D}]})})();