mirror of
https://github.com/wahyd4/cdnjs.git
synced 2026-08-17 08:46:19 +10:00
8 lines
4.0 KiB
JavaScript
8 lines
4.0 KiB
JavaScript
/**
|
|
* Restfull Resources service for AngularJS apps
|
|
* @version v0.4.4 - 2013-04-16
|
|
* @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 r(r){return _.contains(t,r)}var t=["get","head","options","trace"];this.baseUrl="",this.setBaseUrl=function(r){this.baseUrl=r},this.extraFields=[],this.setExtraFields=function(r){this.extraFields=r},this.urlCreator="path",this.setUrlCreator=function(r){if(!_.has(e,r))throw Error("URL Path selected isn't valid");this.urlCreator=r},this.restangularFields={id:"id",route:"route",parentResource:"parentResource",restangularCollection:"restangularCollection"},this.setRestangularFields=function(r){this.restangularFields=_.extend(this.restangularFields,r)},this.responseExtractor=function(r){return r},this.setResponseExtractor=function(r){this.responseExtractor=r};var e={},n=function(r,t){this.baseUrl=r,this.restangularFields=t};n.prototype.base=function(r){var t=this.restangularFields;return this.baseUrl+_.reduce(this.parentsArray(r),function(r,e){var n=r+"/"+e[t.route];return e[t.restangularCollection]||(n+="/"+e[t.id]),n},"")},n.prototype.parentsArray=function(r){for(var t=[];!_.isUndefined(r);)t.push(r),r=r[this.restangularFields.parentResource];return t.reverse()},n.prototype.fetchUrl=function(r,t){return this.base(t)+"/"+r.toLowerCase()},n.prototype.resource=function(r,t,e){return t(this.base(r)+"/:what",{},{getArray:{method:"GET",params:{},isArray:!0,headers:e||{}},get:{method:"GET",params:{},isArray:!1,headers:e||{}},put:{method:"PUT",params:{},isArray:!1,headers:e||{}},post:{method:"POST",params:{},isArray:!1,headers:e||{}},remove:{method:"DELETE",params:{},isArray:!1,headers:e||{}},head:{method:"HEAD",params:{},isArray:!1,headers:e||{}},trace:{method:"TRACE",params:{},isArray:!1,headers:e||{}},options:{method:"OPTIONS",params:{},isArray:!1,headers:e||{}},patch:{method:"PATCH",params:{},isArray:!1,headers:e||{}}})},e.path=n,this.$get=["$resource","$q",function(t,n){function s(r,t,e){if(t[A.route]=e,r){var n=_.chain(A).pick(["id","route","parentResource"]).values().union(m).value();t[A.parentResource]=_.pick(r,n)}return t}function i(r,t,e){var n=s(r,t,e);return n[A.restangularCollection]=!1,n.get=_.bind(d,n),n.getList=_.bind(o,n),n.put=_.bind(l,n),n.post=_.bind(c,n),n.remove=_.bind(h,n),n.head=_.bind(p,n),n.trace=_.bind(f,n),n.options=_.bind(v,n),n.patch=_.bind(b,n),n}function a(r,t,e){var n=s(r,t,e);return n[A.restangularCollection]=!0,n.post=_.bind(c,n,null),n.head=_.bind(p,n),n.trace=_.bind(f,n),n.options=_.bind(v,n),n.patch=_.bind(b,n),n.getList=_.bind(o,n,null),n}function o(r,e,s){var o=r?{what:r}:{},u=this,d=n.defer();return g.resource(this,t,s).getArray(_.extend(o,e),function(t){var e=y(t,"get"),n=_.map(e,function(t){return r?i(u,t,r):i(null,t,u[A.route])});r?d.resolve(a(u,n,r)):d.resolve(a(null,n,u[A.route]))},function(){d.reject(arguments)}),d.promise}function u(e,s,a,o){var u=this,d=n.defer(),h=s||{},l=a||this,c=function(r){var t=y(r,e)||l;"post"!==e?d.resolve(i(u[A.parentResource],t,u[A.route])):d.resolve(i(u,t,h.what))},p=function(){d.reject(arguments)};return r(e)?g.resource(this,t,o)[e](h,c,p):g.resource(this,t,o)[e](h,l,c,p),d.promise}function d(r,t){return _.bind(u,this)("get",r,void 0,t)}function h(r,t){return _.bind(u,this)("remove",r,{},t)}function l(r,t){return _.bind(u,this)("put",r,void 0,t)}function c(r,t,e,n){return _.bind(u,this)("post",_.extend({what:r},e),t,n)}function p(r,t){return _.bind(u,this)("head",r,void 0,t)}function f(r,t){return _.bind(u,this)("trace",r,void 0,t)}function v(r,t){return _.bind(u,this)("options",r,void 0,t)}function b(r,t){return _.bind(u,this)("patch",r,void 0,t)}var g=new e[this.urlCreator](this.baseUrl,this.restangularFields),m=this.extraFields,y=this.responseExtractor,A=this.restangularFields,E={};return E.one=function(r,t){var e={};return e[A.id]=t,i(null,e,r)},E.all=function(r){return a(null,{},r,!0)},E}]}); |