mirror of
https://github.com/wahyd4/cdnjs.git
synced 2026-08-17 08:46:19 +10:00
8 lines
4.1 KiB
JavaScript
8 lines
4.1 KiB
JavaScript
/**
|
|
* Restfull Resources service for AngularJS apps
|
|
* @version v0.5.1 - 2013-04-20
|
|
* @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)}var r=["get","head","options","trace"],e="";this.setBaseUrl=function(t){e=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 o={id:"id",route:"route",parentResource:"parentResource",restangularCollection:"restangularCollection",what:"restangularWhat"};this.setRestangularFields=function(t){o=_.extend(o,t)};var a=function(t){return t};this.setResponseExtractor=function(t){a=t};var s=!0;this.setListTypeIsArray=function(t){s=t};var u=null;this.setRequestSuffix=function(t){u=t};var c={},d=function(){};d.prototype.base=function(t){return e+_.reduce(this.parentsArray(t),function(t,r){var e=t+"/"+r[o.route];return r[o.restangularCollection]||(e+="/"+r[o.id]),e},"")},d.prototype.parentsArray=function(t){for(var r=[];!_.isUndefined(t);)r.push(t),t=t[o.parentResource];return r.reverse()},d.prototype.fetchUrl=function(t,r){return this.base(r)+"/"+t.toLowerCase()},d.prototype.resource=function(t,r,e){var n=u?{restangularSuffix:u}:{};return r(this.base(t)+"/:"+o.what+":restangularSuffix",{},{getList:{method:"GET",params:n,isArray:s,headers:e||{}},get:{method:"GET",params:n,isArray:!1,headers:e||{}},put:{method:"PUT",params:n,isArray:!1,headers:e||{}},post:{method:"POST",params:n,isArray:!1,headers:e||{}},remove:{method:"DELETE",params:n,isArray:!1,headers:e||{}},head:{method:"HEAD",params:n,isArray:!1,headers:e||{}},trace:{method:"TRACE",params:n,isArray:!1,headers:e||{}},options:{method:"OPTIONS",params:n,isArray:!1,headers:e||{}},patch:{method:"PATCH",params:n,isArray:!1,headers:e||{}}})},c.path=d,this.$get=["$resource","$q",function(r,e){function s(t,r,e){if(r[o.route]=e,t){var i=_.chain(o).pick(["id","route","parentResource"]).values().union(n).value();r[o.parentResource]=_.pick(t,i)}return r}function u(t){t.customOperation=_.bind(E,t),_.each(["put","post","get","delete"],function(r){var e="custom"+r.toUpperCase();t[e]=_.bind(E,t,r)}),t.customGETLIST=_.bind(p,t)}function d(t,r,e){var n=s(t,r,e);return n[o.restangularCollection]=!1,n.get=_.bind(v,n),n.getList=_.bind(p,n),n.put=_.bind(b,n),n.post=_.bind(g,n),n.remove=_.bind(m,n),n.head=_.bind(y,n),n.trace=_.bind(A,n),n.options=_.bind(C,n),n.patch=_.bind(R,n),u(r),n}function h(t,r,e){var n=s(t,r,e);return n[o.restangularCollection]=!0,n.post=_.bind(g,n,null),n.head=_.bind(y,n),n.trace=_.bind(A,n),n.options=_.bind(C,n),n.patch=_.bind(R,n),n.getList=_.bind(p,n,null),u(r),n}function l(t){var r={};return t&&(r[o.what]=t),r}function p(t,n,i){var s=l(t),u=this,c=e.defer();return T.resource(this,r,i).getList(_.extend(s,n),function(r){var e=a(r,"getList"),n=_.map(e,function(r){return u[o.restangularCollection]?d(null,r,u[o.route]):d(u,r,t)});u[o.restangularCollection]?c.resolve(h(null,n,u[o.route])):c.resolve(h(u,n,t))},function(){c.reject(arguments)}),c.promise}function f(n,i,s,u){var c=this,h=e.defer(),l=i||{},p=s||this,f=function(t){var r=a(t,n)||p;"post"!==n||c[o.restangularCollection]?h.resolve(d(c[o.parentResource],r,c[o.route])):h.resolve(d(c,r,l[o.what]))},v=function(){h.reject(arguments)};return t(n)?T.resource(this,r,u)[n](l,f,v):T.resource(this,r,u)[n](l,p,f,v),h.promise}function v(t,r){return _.bind(f,this)("get",t,void 0,r)}function m(t,r){return _.bind(f,this)("remove",t,{},r)}function b(t,r){return _.bind(f,this)("put",t,void 0,r)}function g(t,r,e,n){return _.bind(f,this)("post",_.extend(l(t),e),r,n)}function y(t,r){return _.bind(f,this)("head",t,void 0,r)}function A(t,r){return _.bind(f,this)("trace",t,void 0,r)}function C(t,r){return _.bind(f,this)("options",t,void 0,r)}function R(t,r){return _.bind(f,this)("patch",t,void 0,r)}function E(t,r,e,n,i){return _.bind(f,this)(t,_.extend(l(r),e),i,n)}var T=new c[i],L={};return L.one=function(t,r){var e={};return e[o.id]=r,d(null,e,t)},L.all=function(t){return h(null,{},t,!0)},L}]}); |