Files

8 lines
5.8 KiB
JavaScript

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