It is a small library that create object-like and array-like models and collections. I do not want to use anything that already in es5. So this will work only in modern browsers (IE8 100% not supported).
npm install --save moco
bower install btd/moco
Api exports just 2 functions:
var model = model;var User =// this attribute will be used as primary key// just simple attribute// this is a getter and it called each time you try access this model// this will be used to fill undefined attribute while creationvar u1 = _id: 1 name: 'den';console; // 1console; // 1console; // firstname.lastname@example.org; // Unknowu1name = 'daniel';console; // email@example.com
When you call
model() it return new predefined constructor function.
Define new attribute on this model type. Only defined attributes will be saved to model (used
Object.seal). By default created getter and setter for this attribute.
get - it is a function that will be used for getter, if it is defined for this attibute setter will not be defined.
default - it can be anything, that will be assigned to attribute, while creation if its values is undefined. If
default is a function, it will be called, in context of model instance.
primary - it is a boolean field, when it is set to true you will can use this methods:
#isNew() - it will return true|false if primary attribute filled. If primary is false throw exception.
#primary - it is an alias for attribute with primary: true.
It is not possible to create 2 attributes with primary: true and all redefenitions of attributes ignored.
This method can be used to mix something general to model.
This method wrap obj to model if it is not and return it.
For each defined with
.attr attributes will be created getter and setter.
Each defined attribute created enumerable, so you can use this model with any library that work with objects and it will be just works.
Object // ['_id', 'name', 'email', 'title']
If you will try to save anything that is not defined it will not be saved:
u1position = 'developer';u1position // undefined
When model attribute changed it emit event 'change' and 'change:'+attributeName with value and previous value. Also this event can be handled on Model level, as each model type also can emit events.
Model type emit event 'initialize' with create model.
See component/emitter about how to handle events.
Array everywhere it is possible. It have all methods that
Array has and they do everything that native method do. But also it wrap object to Model instance if it is not and emit events:
add emitted with instance when model added to collection.
remove emitter with instance when model removed from colleciton.
It used in the same way as in model types.
var collection = collection;var Users = ;var users = u1 _id: 2 name: 'lisa' ;userslength; // 2// utils.byId add 2 methods byId, removeById, of course Model should have primary attributeusersname // 'daniel'usersname // 'lisa'users// firstname.lastname@example.org// email@example.com
There are 3 built in collection plugins:
It create internal cache for primary attribute of models it contains and add methods
Nothing in this plugin checks that collection contains 2 model with the same primary attribute.
It create internal cache for specified attribute and add method
#by[Attr](attr) [Attr] there it is name with capital letter.
It add ability to collection emit
change event when nested model changed.
It emit change event every time nested model or collection changed. It can works together with
collection.modelsChanges and proxy these events too.
var NestedModel1 = moco;var NestedCollection = moco;var NestedModel2 = moco;var nm = a: 'a' ;var c = nm ;var m = b: c ;/*this will emit 4 events on m:change:b.a with value 'b' and prev 'a'change:b with value === prev === cchange with previous 2 events*/nma = 'b';
The MIT License (MIT)
Copyright (c) 2013 Denis Bardadym
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.