Files

8 lines
4.7 KiB
JavaScript

/**
* Restfull Resources service for AngularJS apps
* @version v0.5.4 - 2013-04-26
* @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 a={id:"id",route:"route",parentResource:"parentResource",restangularCollection:"restangularCollection",what:"restangularWhat"};this.setRestangularFields=function(t){a=_.extend(a,t)};var o=function(t){return t};this.setResponseExtractor=function(t){o=t},this.setResponseInterceptor=this.setResponseExtractor;var s=function(t){return t};this.setOnElemRestangularized=function(t){s=t};var u=!0;this.setListTypeIsArray=function(t){u=t};var d=null;this.setRequestSuffix=function(t){d=t};var c={},h=function(){};h.prototype.base=function(t){return r+_.reduce(this.parentsArray(t),function(t,e){var r=t+"/"+e[a.route];return e[a.restangularCollection]||(r+="/"+e[a.id]),r},"")},h.prototype.parentsArray=function(t){for(var e=[];!_.isUndefined(t);)e.push(t),t=t[a.parentResource];return e.reverse()},h.prototype.fetchUrl=function(t,e){var r=this.base(t);return e[a.what]&&(r+="/"+e[a.what]),r},h.prototype.resource=function(t,e,r){var n=d?{restangularSuffix:d}:{};return e(this.base(t)+"/:"+a.what+":restangularSuffix",{},{getList:{method:"GET",params:n,isArray:u,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=h,this.$get=["$resource","$q",function(e,r){function u(t,e,r){if(e[a.route]=r,e.addRestangularMethod=_.bind(w,e),t){var i=_.chain(a).pick(["id","route","parentResource"]).values().union(n).value();e[a.parentResource]=_.pick(t,i)}return e}function d(t,e,r){var n={};return n[a.id]=r,p(t,n,e)}function h(t,e){return f(t,{},e,!0)}function l(t){t.customOperation=_.bind(x,t),_.each(["put","post","get","delete"],function(e){_.each(["do","custom"],function(r){var n=r+e.toUpperCase();t[n]=_.bind(x,t,e)})}),t.customGETLIST=_.bind(m,t),t.doGETLIST=t.customGETLIST}function p(t,e,r){var n=u(t,e,r);return n[a.restangularCollection]=!1,n.get=_.bind(g,n),n.getList=_.bind(m,n),n.put=_.bind(R,n),n.post=_.bind(A,n),n.remove=_.bind(y,n),n.head=_.bind(E,n),n.trace=_.bind(T,n),n.options=_.bind(C,n),n.patch=_.bind(L,n),n.one=_.bind(d,n,n),n.all=_.bind(h,n,n),l(n),s(n,!1,r)}function f(t,e,r){var n=u(t,e,r);return n[a.restangularCollection]=!0,n.post=_.bind(A,n,null),n.head=_.bind(E,n),n.trace=_.bind(T,n),n.options=_.bind(C,n),n.patch=_.bind(L,n),n.getList=_.bind(m,n,null),l(n),s(n,!0,r)}function v(t){var e={};return t&&(e[a.what]=t),e}function m(t,n,i){var s=v(t),u=this,d=r.defer();return U.resource(this,e,i).getList(_.extend(s,n),function(e){var r=o(e,"getList",t,U.fetchUrl(u,s)),n=_.map(r,function(e){return u[a.restangularCollection]?p(null,e,u[a.route]):p(u,e,t)});u[a.restangularCollection]?d.resolve(f(null,n,u[a.route])):d.resolve(f(u,n,t))},function(t){d.reject(t)}),d.promise}function b(n,i,s,u){var d=this,c=r.defer(),h=i||{},l=s||this,f=function(t){var e=o(t,n,h[a.what]||d[a.route],U.fetchUrl(d,h))||l;"post"!==n||d[a.restangularCollection]?c.resolve(p(d[a.parentResource],e,d[a.route])):c.resolve(p(d,e,h[a.what]))},_=function(t){c.reject(t)};return t(n)?U.resource(this,e,u)[n](h,f,_):U.resource(this,e,u)[n](h,l,f,_),c.promise}function g(t,e){return _.bind(b,this)("get",t,void 0,e)}function y(t,e){return _.bind(b,this)("remove",t,{},e)}function R(t,e){return _.bind(b,this)("put",t,void 0,e)}function A(t,e,r,n){return _.bind(b,this)("post",_.extend(v(t),r),e,n)}function E(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 C(t,e){return _.bind(b,this)("options",t,void 0,e)}function L(t,e){return _.bind(b,this)("patch",t,void 0,e)}function x(t,e,r,n,i){return _.bind(b,this)(t,_.extend(v(e),r),i,n)}function w(t,e,r,n,i,a){var o;o="getList"===e?_.bind(m,this,r):_.bind(x,this,e,r),this[t]=function(t,e,r){var s=_.defaults({params:t,headers:e,elem:r},{params:n,headers:i,elem:a});return o(s.params,s.headers,s.elem)}}var U=new c[i],S={};return S.one=_.bind(d,S,null),S.all=_.bind(h,S,null),S}]});