forked from mkuklis/backbone.service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone.service.min.js
1 lines (1 loc) · 2.18 KB
/
backbone.service.min.js
1
(function(t){"object"==typeof exports?module.exports=t(require("underscore"),require("backbone")):"function"==typeof define&&define.amd?define(["underscore","backbone"],t):t(_,Backbone)})(function(t,e){"use strict";function s(s){var n="sync_";this[n]={},this.options=s||{};var i=r(this.options.sync||{});t(i).each(t.partial(this.createMethod,this[n]),this),this.targets=r(this.options.targets||{}),t(this.targets).each(t.partial(this.createMethod,this),this),this.sync=function(s,r,i){var o=this[n][s];return t.isFunction(o)?o.call(this,{},i):e.sync(s,r,i)}}function r(e){var s,r="GET";return t(e).map(function(e,n){return s={name:n,path:e,method:r},t.isObject(e)&&!t.isFunction(e)&&t.extend(s,{path:e.path,method:e.method||r,data:e.data}),s})}function n(t){this.context=t||this,this.success=[],this.error=[]}s.sendModels=!0,s.url="",s.prototype.createMethod=function(r,o){var a,c,h=this;r[o.name]=function(r,u){return a=new n(h),r=t.isFunction(o.data)?o.data(this,r):s.sendModels&&"read"!=i[o.method.toUpperCase()]?t.extend(this.toJSON(),r,o.data):t.extend({},r,o.data),u=h.createOptions(a,o,u,this),r&&!t.isEmpty(r)&&(e.emulateJSON||"read"==i[o.method.toUpperCase()]||(r=JSON.stringify(r),u.contentType="application/json"),u.data=r),u.type=o.method.toUpperCase(),c=t.find(t.values(i),function(t){return t===o.name})?o.name:i[u.type],e.sync(c,this,u),a}},s.prototype.createOptions=function(e,r,n,i){var o=this;n||(n={});var a;a=t.isFunction(r.path)?r.path(i,n):r.path;var c=t.result(this.options,"url")||s.url;return{url:c+a,success:function(t){n.success&&n.success.call(o,t),e.resolve(t)},error:function(t,s,r){n.error&&n.error.apply(o,[r,t]),e.reject(r,t)}}};var i={POST:"create",PUT:"update",DELETE:"delete",GET:"read"};t.extend(s.prototype,e.Events),e.Service=s,n.prototype={constructor:n,then:function(t,e){return t&&(this.resolved?t.apply(this.context,this.resolved):this.success.push(t)),e&&(this.rejected?e.apply(this.context,this.rejected):this.error.push(e)),this},resolve:function(){var t;for(this.resolved=arguments,this.error=[];t=this.success.shift();)t.apply(this.context,this.resolved)},reject:function(){var t;for(this.rejected=arguments,this.success=[];t=this.error.shift();)t.apply(this.context,this.rejected)}}});