modella
Version:
simplified models
19 lines (18 loc) • 8.76 kB
JavaScript
(function(){function c(b,a,g){var d=c.resolve(b);if(null==d)throw g=g||b,a=a||"root",d=Error('Failed to require "'+g+'" from "'+a+'"'),d.path=g,d.parent=a,d.require=!0,d;a=c.modules[d];a._resolving||a.exports||(g={exports:{}},g.client=g.component=!0,a._resolving=!0,a.call(this,g.exports,c.relative(d),g),delete a._resolving,a.exports=g.exports);return a.exports}c.modules={};c.aliases={};c.resolve=function(b){"/"===b.charAt(0)&&(b=b.slice(1));for(var a=[b,b+".js",b+".json",b+"/index.js",b+"/index.json"],
g=0;g<a.length;g++){b=a[g];if(c.modules.hasOwnProperty(b))return b;if(c.aliases.hasOwnProperty(b))return c.aliases[b]}};c.normalize=function(b,a){var c=[];if("."!=a.charAt(0))return a;b=b.split("/");a=a.split("/");for(var d=0;d<a.length;++d)".."==a[d]?b.pop():"."!=a[d]&&""!=a[d]&&c.push(a[d]);return b.concat(c).join("/")};c.register=function(b,a){c.modules[b]=a};c.alias=function(b,a){if(!c.modules.hasOwnProperty(b))throw Error('Failed to alias "'+b+'", it does not exist');c.aliases[a]=b};c.relative=
function(b){function a(d){var g=a.resolve(d);return c(g,b,d)}var g=c.normalize(b,"..");a.resolve=function(a){var k=a.charAt(0);if("/"==k)return a.slice(1);if("."==k)return c.normalize(g,a);var k=b.split("/"),e;a:{for(e=k.length;e--;)if("deps"===k[e])break a;e=-1}(e+=1)||(e=0);return a=k.slice(0,e+1).join("/")+"/deps/"+a};a.exists=function(b){return c.modules.hasOwnProperty(a.resolve(b))};return a};c.register("component-indexof/index.js",function(b,a,c){c.exports=function(a,b){if(a.indexOf)return a.indexOf(b);
for(var e=0;e<a.length;++e)if(a[e]===b)return e;return-1}});c.register("component-emitter/index.js",function(b,a,c){function d(a){if(a){for(var b in d.prototype)a[b]=d.prototype[b];return a}}var k=a("indexof");c.exports=d;d.prototype.on=function(a,b){this._callbacks=this._callbacks||{};(this._callbacks[a]=this._callbacks[a]||[]).push(b);return this};d.prototype.once=function(a,b){function f(){c.off(a,f);b.apply(this,arguments)}var c=this;this._callbacks=this._callbacks||{};b._off=f;this.on(a,f);return this};
d.prototype.off=d.prototype.removeListener=d.prototype.removeAllListeners=function(a,b){this._callbacks=this._callbacks||{};if(0==arguments.length)return this._callbacks={},this;var f=this._callbacks[a];if(!f)return this;if(1==arguments.length)return delete this._callbacks[a],this;var c=k(f,b._off||b);~c&&f.splice(c,1);return this};d.prototype.emit=function(a){this._callbacks=this._callbacks||{};var b=[].slice.call(arguments,1),f=this._callbacks[a];if(f)for(var f=f.slice(0),c=0,m=f.length;c<m;++c)f[c].apply(this,
b);return this};d.prototype.listeners=function(a){this._callbacks=this._callbacks||{};return this._callbacks[a]||[]};d.prototype.hasListeners=function(a){return!!this.listeners(a).length}});c.register("ForbesLindesay-is-browser/client.js",function(b,a,c){c.exports=!0});c.register("modella/index.js",function(b,a,c){b=c.exports=a("./lib/model");b.utils={};b.utils.clone=a("./lib/utils/clone");b.utils.type=a("./lib/utils/type")});c.register("modella/lib/model.js",function(b,a,c){var d=a("./proto"),k=
a("./static"),e=a("./utils/clone");try{var h=a("emitter")}catch(f){h=a("emitter-component")}c.exports=function(a){function b(a){if(!(this instanceof b))return new b(a);if(Array.isArray(a)){for(var f=[],c=0;c<a.length;++c)f.push(new b(a[c]));return f}a=a?e(a):{};this.attrs={};this._callbacks={};this.dirty={};this.errors=[];b.emit("initializing",this,a);for(f in b._defaults)b.attrs[f]&&(this.attrs[f]=e(b._defaults[f]));for(f in a)b.attrs[f]&&(this.attrs[f]=a[f]);b.emit("initialize",this)}if("string"!==
typeof a)throw new TypeError("model name required");h(b);b.modelName=a;b.base="/"+a.toLowerCase();b.attrs={};b.validators=[];b._defaults={};for(var f in k)b[f]=k[f];b.prototype={};b.prototype.model=b;for(f in d)b.prototype[f]=d[f];return b}});c.register("modella/lib/proto.js",function(b,a,c){var d=a("./utils/clone"),k=function(){};try{var e=a("emitter")}catch(h){e=a("emitter-component")}e(b);b.error=function(a,b){this.errors.push({attr:a,message:b});return this};b.isNew=function(){return!this.has(this.model.primaryKey)};
b.primary=function(a){var b=this.model.primaryKey;return 0==arguments.length?this[b]():this[b](a)};b.isValid=function(){this.validate();return 0==this.errors.length};b.changed=function(a){if(1==arguments.length)return!!this.dirty[a];var b={};for(a in this.dirty)b[a]=this.dirty[a];return b};b.validate=function(){var a=this.model.validators||[];this.errors=[];for(var b=0,c=a.length;b<c;b++)a[b](this);this.errors.length?(this.model.emit("invalid",this,this.errors),this.emit("invalid",this.errors)):(this.model.emit("valid",
this,null),this.emit("valid",null))};b.remove=function(){function a(b,f){if(b)return d.emit("error",b),c(b);d.removed=!0;d.model.emit("remove",d);d.emit("remove");c(null,d)}var b=[].slice.call(arguments),c=b.pop()||k,d=this,h=this.model.remove;!this.model.remove&&this.model._sync&&(h=this.model._sync.remove);if(this.isNew())return c(Error("not saved"));this.run("removing",function(){h.apply(d,b.concat(a))})};b.save=function(){function b(a,f){if(a)return h.emit("error",a),c(a);if(f){h.primary(f.id||
f._id);for(var d in h.model.attrs)h.attrs[d]=f[d]}h.dirty={};e&&(h.model.emit("create",h),h.emit("create"));h.model.emit("save",h);h.emit("save");c(null,h)}var a=[].slice.call(arguments),c=a.pop()||k,d="save",h=this,e=this.isNew();if(!e){var g=this.changed(),d="update";if(!g)return c(null,this)}var l=this.model[d];!this.model[d]&&this.model._sync&&(l=this.model._sync[d]);if(!this.isValid())return this.primary(null),c(Error("validation failed"));this.run("saving",function(d){if(d)return c(d);e?h.run("creating",
function(d){if(d)return c(d);l.apply(h,a.concat(b))}):l.apply(h,a.concat(b))})};b.set=function(a){this.model.emit("setting",this,a);this.emit("setting",a);for(var b in a)if(this.model.attrs[b])this[b](a[b]);return this};b.get=function(a){return this.attrs[a]};b.has=function(a){return void 0!==this.attrs[a]};b.toJSON=function(){return d(this.attrs)};b.run=function(a,b){function c(g){if(g)return b(g);if("saving"===a&&(e.errors.length||!e.isValid()))return b(Error("validation failed"));d.length?d.shift().call(e,
e,c):h.length?h.shift().call(e,c):b(g)}var d=this.model.listeners(a),h=this.listeners(a),e=this;e.errors=[];c()}});c.register("modella/lib/static.js",function(b,a,c){var d=a("./utils/clone"),k=a("./utils/type"),e=a("is-browser");b.validate=function(a){this.validators.push(a);return this};b.use=function(a,b){b||(b=a,a=void 0);if(a){var c=/browser|client/,d=/server|node|node.js/;e&&c.test(a)?b(this):!e&&d.test(a)&&b(this)}else b(this);return this};b.attr=function(a,b){if(this.attrs[a])for(var c in b)this.attrs[a][c]=
b[c];else{this.attrs[a]=b||{};if("_id"===a||"id"===a)this.attrs[a].primaryKey=!0,this.primaryKey=a;this.prototype[a]=function(b){if(0===arguments.length)return d(this.attrs[a]);var c=this.attrs[a];b=d(b);var e=!1,e=k(b);if(e="object"==e||"array"==e?!0:this.attrs[a]!=b)this.dirty[a]=b,this.attrs[a]=b,this.model.emit("change",this,a,b,c),this.model.emit("change "+a,this,b,c),this.emit("change",a,b,c),this.emit("change "+a,b,c);return this}}b&&void 0!==b.defaultValue&&(this._defaults[a]=b.defaultValue);
this.emit("attr",a,b);return this}});c.register("modella/lib/utils/clone.js",function(b,a,c){function d(a){switch(k(a)){case "object":if(a.toHexString)return a;var b={},c;for(c in a)b[c]=d(a[c]);return b;case "array":b=Array(a.length);c=0;for(var g=a.length;c<g;c++)b[c]=d(a[c]);return b;case "regexp":return b=""+(a.multiline?"m":""),b+=a.global?"g":"",b+=a.ignoreCase?"i":"",RegExp(a.source,b);case "date":return new Date(a.getTime());default:return a}}var k=a("./type");c.exports=d});c.register("modella/lib/utils/type.js",
function(b,a,c){var d=Object.prototype.toString;c.exports=function(a){switch(d.call(a)){case "[object Function]":return"function";case "[object Date]":return"date";case "[object RegExp]":return"regexp";case "[object Arguments]":return"arguments";case "[object Array]":return"array"}return null===a?"null":void 0===a?"undefined":a===Object(a)?"object":typeof a}});c.alias("component-emitter/index.js","modella/deps/emitter/index.js");c.alias("component-emitter/index.js","emitter/index.js");c.alias("component-indexof/index.js",
"component-emitter/deps/indexof/index.js");c.alias("ForbesLindesay-is-browser/client.js","modella/deps/is-browser/client.js");c.alias("ForbesLindesay-is-browser/client.js","modella/deps/is-browser/index.js");c.alias("ForbesLindesay-is-browser/client.js","is-browser/index.js");c.alias("ForbesLindesay-is-browser/client.js","ForbesLindesay-is-browser/index.js");"object"==typeof exports?module.exports=c("modella"):"function"==typeof define&&define.amd?define(function(){return c("modella")}):this.modella=
c("modella")})();