UNPKG

backbone-data

Version:

A simple data store for backbone models and collections inspired by Ember Data and angular-data.

1 lines 2.89 kB
!function(e){"use strict";var t=function(t){function n(e,t){var n,r=l[e].idAttribute;s[e]=s[e]||{},_.isArray(t)?t.forEach(function(t){var n=t[r];s[e][n]=!0}):(n=t[r],s[e][n]=!0)}function r(e,t){return s[e]?s[e][t]:!1}function o(e){return l[e].collection?!1:!0}function i(e){var t=new $.Deferred,n=this.get(e);return d[e]?t.resolve(n):n.fetch().then(function(){d[e]=!0,t.resolve(n)},function(e){t.reject(JSON.parse(e.responseText))}),t.promise()}function c(e,t){var n,o,i,c={},f=$.Deferred();return i=this.get(e,t),n=l[e].idAttribute,i?r(e,t)?i.fetch().then(function(){return delete s[e][t],i}):(f.resolve(i),f.promise()):(c[n]=t,o=new l[e].model(c),o.fetch().then(function(){return u.inject(e,o),o},function(e){return JSON.parse(e.responseText)}))}var u=new t.Model,l={},f={},s={},a={},d={};return u.defineResource=function(e){if(!e.hasOwnProperty("name")||!e.name)throw new Error("name must be specified when defining a resource");if((!e.hasOwnProperty("idAttribute")||!e.idAttribute)&&e.collection)throw new Error("idAttribute must be specified when defining a resource");if(l[e.name])throw new Error(e.name+" resource has already been defined!");return!e.model&&e.collection&&(e.model=e.collection.prototype.model),e.hasOwnProperty("idAttribute")&&!e.hasOwnProperty("collection")&&(e.collection=t.Collection),l[e.name]=e,f[e.name]||(e.collection?f[e.name]=new e.collection:(e.model=e.model||t.Model,f[e.name]=new e.model)),this},u.createInstance=function(e){return new l[e].model},u.inject=function(e,r,o){var i,c;return f[e]instanceof t.Collection?(o=_.extend({incomplete:!1},o),i=f[e],o.incomplete&&n(e,r),i.add(r,{merge:!0})):(c=f[e],c.set(r))},u.get=function(e,t){var n,r,i,c={};return arguments[0]in this.attributes?this.attributes[arguments[0]]:o(e)?f[e]:(i=l[e].idAttribute,r=f[e],r&&(c[i]=t,n=r.findWhere(c))?n:null)},u.getAll=function(e){return f[e]},u.ejectAll=function(e){var t=f[e];t&&t.reset()},u.find=function(e,t){return o(e)?i.call(this,e):c.call(this,e,t)},u.findAll=function(e,t){var n,r=f[e],o=$.Deferred();return"completed"===a[e]?(o.resolve(r),o.promise()):(n=new l[e].collection,n.fetch({success:function(){u.inject(e,n.toJSON(),t),a[e]="completed",o.resolve(r)},error:function(){throw new Error("DS error fetching collection: "+e)}}),o.promise())},u.where=function(e,t){var n=f[e],r=l[e].collection,o=new r,i=n.where(t);return o.add(i),o},u.filter=function(e,t){var n=f[e],r=l[e].collection,o=new r,i=n.filter(t);return o.add(i),o},u.update=function(e,t,n){var r=this.get(e,t);return r.set(n),r.save().then(function(){return r})},u.create=function(e,t){l[e].idAttribute;return t.save().then(function(){return f[e].add(t),t})},u.destroy=function(e,t){var n=this.get(e,t);return n.destroy()},u.reset=function(){return f={},l={},s={},a={},d={},this},e.DS=u,u};"function"==typeof define&&define.amd?define(["backbone"],function(e){return t(e)}):t(e.Backbone)}(window);