UNPKG

backbone-http

Version:

An HTTP interface for BackboneORM

8 lines (7 loc) 6.55 kB
/* backbone-http.js 0.5.0 Copyright (c) 2013 Vidigami - https://github.com/vidigami/backbone-http License: MIT (http://www.opensource.org/licenses/mit-license.php) Dependencies: Backbone.js, Underscore.js, Moment.js, Inflection.js, BackboneORM, and Superagent. */ (function(){var a={};(function(){"use strict";var b={},c={},d=function(a,b){return{}.hasOwnProperty.call(a,b)},e=function(a,b){var c,d,e=[];c=/^\.\.?(\/|$)/.test(b)?[a,b].join("/").split("/"):b.split("/");for(var f=0,g=c.length;g>f;f++)d=c[f],".."===d?e.pop():"."!==d&&""!==d&&e.push(d);return e.join("/")},f=function(a){return a.split("/").slice(0,-1).join("/")},g=function(b){var c=function(c){var d=f(b),g=e(d,c);return a.require(g,b)};return c.register=a.require.register,c.shim=a.require.shim,c},h=function(a,b){var d={id:a,exports:{}};return c[a]=d,b(d.exports,g(a),d),d.exports},i=function(a,f){var g=e(a,".");if(null==f&&(f="/"),d(c,g))return c[g].exports;if(d(b,g))return h(g,b[g]);var i=e(g,"./index");if(d(c,i))return c[i].exports;if(d(b,i))return h(i,b[i]);throw new Error('Cannot find module "'+a+'" from "'+f+'"')},j=function(a,c){if("object"==typeof a)for(var e in a)d(a,e)&&(b[e]=a[e]);else b[a]=c},k=Object.prototype.toString,l=function(a){return"[object Array]"===k.call(a)},m=function(b){if("undefined"!=typeof window){l(b)||(b=[b]);for(var c=function(b){var c;try{if(a.require(b.path))return}catch(d){}try{c="function"==typeof window.require?window.require(b.path):void 0}catch(d){}if(!c&&b.symbol){var e=b.symbol.split(".");c=window;for(var f=0,g=e.length;g>f&&(c=c[e[f]]);f++);}if(!c&&b.symbol_path&&window.require){var e=b.symbol_path.split("."),h=e.shift();try{c="function"==typeof window.require?window.require(h):void 0}catch(d){}for(var f=0,g=e.length;g>f&&(c=null!=c?c[e[f]]:void 0);f++);}if(!c){if(b.optional)return;throw new Error("Missing dependency: "+b.path)}a.require.register(b.path,function(a,b,d){return d.exports=c}),b.alias&&a.require.register(b.alias,function(a,b,d){return d.exports=c})},d=0,e=b.length;e>d;d++)c(b[d])}};a.require=i,a.require.register=j,a.require.shim=m}).call(this);var b=a.require;if(b.register("backbone-http/lib/cursor",function(a,b,c){var d,e,f,g,h,i={}.hasOwnProperty,j=function(a,b){function c(){this.constructor=a}for(var d in b)i.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};g=b("underscore"),d=b("backbone-orm").Cursor,f=b("backbone-orm").JSONUtils,c.exports=e=function(a){function b(){return h=b.__super__.constructor.apply(this,arguments)}return j(b,a),b.prototype.toJSON=function(a){var b,c=this;return this.hasCursorQuery("$zero")?a(null,this.hasCursorQuery("$one")?null:[]):this.request.get(this.url).query(b=f.toQuery(g.extend(g.clone(this._find),this._cursor))).type("json").end(function(d,e){var g;return d?a(d):b.$one&&404===e.status?a(null,null):e.ok?(g=f.parse(e.body),a(null,c.hasCursorQuery("$count")||c.hasCursorQuery("$exists")?g.result:g)):a(new Error("Ajax failed with status "+e.status+" with: "+JSON.stringify(e.body)))})},b}(d)}),b.register("backbone-http/lib/index",function(a,b,c){var d,e,f,g,h;for("undefined"!=typeof window&&null!==window&&b.shim&&b.shim([{symbol:"_",path:"lodash",alias:"underscore",optional:!0},{symbol:"_",path:"underscore"},{symbol:"Backbone",path:"backbone"},{symbol:"moment",path:"moment"},{symbol:"inflection",path:"inflection"},{symbol:"stream",path:"stream",optional:!0},{symbol:"Backbone.ORM",symbol_path:"backbone.ORM",path:"backbone-orm"},{symbol:"superagent",path:"superagent"}]),c.exports={sync:b("./sync"),modules:{"backbone-orm":d=b("backbone-orm"),superagent:b("superagent")}},h=["url","querystring","lru-cache","underscore","backbone","moment","inflection","stream"],f=0,g=h.length;g>f;f++)e=h[f],c.exports.modules[e]=d.modules[e]}),b.register("backbone-http/lib/sync",function(a,b,c){var d,e,f,g,h,i,j,k,l;l=b("underscore"),d=b("backbone"),k=b("backbone-orm"),i=k.Schema,j=k.Utils,g=k.JSONUtils,h=k.CacheSingletons.ModelCache,e=b("./cursor"),c.exports=f=function(){function a(a){if(this.model_type=a,this.model_type.model_name=j.findOrGenerateModelName(this.model_type),!(this.url=l.result(this.model_type.prototype,"url")))throw new Error("Missing url for model: "+this.model_type);this.schema=new i(this.model_type),this.request=b("superagent")}return a.prototype.initialize=function(){return this.is_initialized?void 0:(this.is_initialized=!0,this.schema.initialize())},a.prototype.resetSchema=function(a,b){return this.request.del(this.url).end(function(a,c){return a?b(a):c.ok?b():b(new Error("Ajax failed with status "+c.status+" for destroy with: "+JSON.stringify(c.body)))})},a.prototype.cursor=function(a){return null==a&&(a={}),new e(a,{model_type:this.model_type,url:this.url,request:this.request})},a.prototype.destroy=function(a,b){return this.request.del(this.url).query(a).end(function(a,c){return a?b(a):c.ok?b():b(new Error("Ajax failed with status "+c.status+" for destroy with: "+JSON.stringify(c.body)))})},a}(),c.exports=function(a){var b,d,e;return j.isCollection(new a)?(b=j.configureCollectionModelType(a,c.exports),a.prototype.sync=b.prototype.sync):(d=new f(a),a.prototype.sync=e=function(a,b,e){var f,h,i;if(null==e&&(e={}),d.initialize(),"createSync"===a)return c.exports.apply(null,Array.prototype.slice.call(arguments,1));if("sync"===a)return d;if("schema"===a)return d.schema;if("isRemote"===a)return!0;if(l.contains(["create","update","patch","delete","read"],a)){if(!(i=e.url||l.result(b,"url")))throw new Error("Missing url for model");switch(h=d.request,a){case"read":f=h.get(i).query({$one:!b.models}).type("json");break;case"create":f=h.post(i).send(e.attrs||b.toJSON(e)).type("json");break;case"update":f=h.put(i).send(e.attrs||b.toJSON(e)).type("json");break;case"patch":f=h.patch(i).send(e.attrs||b.toJSON(e)).type("json");break;case"delete":f=h.del(i)}return f.end(function(b,c){return b?e.error(b):c.ok?e.success(g.parse(c.body)):e.error(new Error("Ajax failed with status "+c.status+" for "+a+" with: "+JSON.stringify(c.body)))}),void 0}return d[a]?d[a].apply(d,Array.prototype.slice.call(arguments,1)):void 0},j.configureModelType(a),h.configureSync(a,e))}}),"object"==typeof exports)module.exports=b("backbone-http/lib/index");else if("function"==typeof define&&define.amd)define("backbone-http",["backbone-orm","superagent"],function(){return b("backbone-http/lib/index")});else{var c=this.Backbone;if(!c&&"function"==typeof window.require)try{c=window.require("backbone")}catch(d){c=this.Backbone={}}c.HTTP=b("backbone-http/lib/index")}}).call(this);