mirror of
https://github.com/wahyd4/cdnjs.git
synced 2026-08-19 01:36:01 +10:00
8 lines
6.0 KiB
JavaScript
8 lines
6.0 KiB
JavaScript
/**
|
|
* Restfull Resources service for AngularJS apps
|
|
* @version v0.6.6 - 2013-05-21
|
|
* @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)&&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(m,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 p=!0;this.setListTypeIsArray=function(t){p=t};var f=null;this.setRequestSuffix=function(t){f=t};var v=["$delete","$get","$getList","$head","$options","$patch","$post","$put","$query","$remove","$save","$trace"],m={},b=function(){};b.prototype.parentsArray=function(t){for(var e=[];!_.isUndefined(t);)e.push(t),t=t[c.parentResource];return e.reverse()},b.prototype.resource=function(t,r,n){var i={};return r(this.base(t)+"/:"+c.what+(f||""),{},{getList:e({method:"GET",params:i,isArray:p,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 g=function(){};g.prototype=new b,g.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},"")},g.prototype.fetchUrl=function(t,e){var r=this.base(t);return e[c.what]&&(r+="/"+e[c.what]),r},m.path=g,this.$get=["$resource","$q",function(e,r){function n(t,e,r){if(e[c.route]=r,e.addRestangularMethod=_.bind(q,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,C(t,n,e)}function s(t,e){return T(t,{},e,!0)}function p(t,e){return t.call=_.bind(f,t),t.get=_.bind(b,t),t[c.restangularCollection]=e,e&&(t.push=_.bind(f,t,"push")),t}function f(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)}),p(e.promise,this[c.restangularCollection])}function b(t){var e=r.defer();return this.then(function(r){e.resolve(r[t])}),p(e.promise,this[c.restangularCollection])}function g(t){return _.omit(t,_.values(_.omit(c,"id")))}function y(t){t.customOperation=_.bind(G,t),_.each(["put","post","get","delete"],function(e){_.each(["do","custom"],function(r){var n=r+e.toUpperCase();t[n]=_.bind(G,t,e)})}),t.customGETLIST=_.bind(E,t),t.doGETLIST=t.customGETLIST}function R(t){var e=angular.copy(t);return C(e[c.parentResource],e,e[c.route])}function C(t,e,r){var i=n(t,e,r);return i=_.omit(i,v),i[c.restangularCollection]=!1,i.get=_.bind($,i),i.getList=_.bind(E,i),i.put=_.bind(x,i),i.post=_.bind(U,i),i.remove=_.bind(w,i),i.head=_.bind(I,i),i.trace=_.bind(O,i),i.options=_.bind(P,i),i.patch=_.bind(S,i),i.one=_.bind(o,i,i),i.all=_.bind(s,i,i),y(i),l(i,!1,r)}function T(t,e,r){var i=n(t,e,r);return i[c.restangularCollection]=!0,i.post=_.bind(U,i,null),i.head=_.bind(I,i),i.trace=_.bind(O,i),i.options=_.bind(P,i),i.patch=_.bind(S,i),i.getList=_.bind(E,i,null),y(i),l(i,!0,r)}function A(t){var e={};return t&&(e[c.what]=t),e}function E(t,n,i){var o=A(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]?C(null,e,s[c.route]):C(s,e,t)});n=_.extend(r,n),s[c.restangularCollection]?a.resolve(T(null,n,s[c.route])):a.resolve(T(s,n,t))},function(t){a.reject(t)}),p(a.promise,!0)}function L(n,i,o,s){var u=this,l=r.defer(),f=i||{},v=o||this,m=f[c.what]||this[c.route],b=H.fetchUrl(this,f),y=o||g(this);y=h(y,n,m,b);var R=function(t){var e=d(t,n,m,b)||v;"post"!==n||u[c.restangularCollection]?l.resolve(C(u[c.parentResource],e,u[c.route])):l.resolve(C(u,e,f[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](f,{},R,T):H.resource(this,e,E)[A](f,R,T):H.resource(this,e,E)[A](f,y,R,T),p(l.promise)}function $(t,e){return _.bind(L,this)("get",t,void 0,e)}function w(t,e){return _.bind(L,this)("remove",t,{},e)}function x(t,e){return _.bind(L,this)("put",t,void 0,e)}function U(t,e,r,n){return _.bind(L,this)("post",_.extend(A(t),r),e,n)}function I(t,e){return _.bind(L,this)("head",t,void 0,e)}function O(t,e){return _.bind(L,this)("trace",t,void 0,e)}function P(t,e){return _.bind(L,this)("options",t,void 0,e)}function S(t,e){return _.bind(L,this)("patch",t,void 0,e)}function G(t,e,r,n,i){return _.bind(L,this)(t,_.extend(A(e),r),i,n)}function q(t,e,r,n,i,o){var s;s="getList"===e?_.bind(E,this,r):_.bind(G,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 m[u],D={};return D.copy=_.bind(R,D),D.one=_.bind(o,D,null),D.all=_.bind(s,D,null),D}]}); |