Moco
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).
Installation
Node:
npm install --save moco
Browser:
bower install btd/moco
Usage
Api exports just 2 functions: model
and collection
.
Models
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 creation var u1 = _id: 1 name: 'den';console; // 1console; // 1 console; // den@example.comconsole; // Unknow u1name = 'daniel'; console; // daniel@example.com
When you call model()
it return new predefined constructor function.
.attr(name[, options])
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.
Supported options:
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.
.use(fn)
This method can be used to mix something general to model.
.create(obj)
This method wrap obj to model if it is not and return it.
#[attr]
For each defined with .attr
attributes will be created getter and setter.
Enumerable properties and defined attributes
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
Events
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.
Collection
Collection mimic javascript native 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.
.use(fn)
It used in the same way as in model types.
Example
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// daniel@example.com// lisa@example.com
Collection plugins
There are 3 built in collection plugins:
collection.byId
It create internal cache for primary attribute of models it contains and add methods #byId(primary)
and #removeById(primary)
.
Nothing in this plugin checks that collection contains 2 model with the same primary attribute.
collection.byField(attr)
It create internal cache for specified attribute and add method #by[Attr](attr)
[Attr] there it is name with capital letter.
collection.modelsChanges
It add ability to collection emit change
event when nested model changed.
model.nestedObjects
It emit change event every time nested model or collection changed. It can works together with collection.modelsChanges
and proxy these events too.
Small example:
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 === c change with previous 2 events*/nma = 'b';
License
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.