modella
Highly extendable bloat-free models.
Installation
With node.js:
npm install modella
In the browser (using component):
component install modella/modella
Philosophy
modella
is a bare bones model. It exposes a few APIs on which plugins can be
built to enhance functionality. Usage of these plugins enables high-powered but
bloat free models.
Check out the list of available plugins
Defining Models
All modella
definition methods are chainable.
modella( name )
Creates a new model with the name name
.
var modella = ;var User = ;
Model.use ( [env], fn )
As mentioned in the philosophy, modella
's goal is to make it easy to extend
models with plugins. This lets you create models that do exactly what you need,
and nothing more. You can use env
to target the client and the server separately.
var modella = ;var validators = ;var User = ; User;
Browser plugin:
User;// orUser;
Server plugin:
User;// orUser;
Model.attr( name, [options] )
Adds attribute attrName
to a model. Additional options
can be passed in as
an object. Modella itself accepts the option defaultValue
to specify the default value for the attribute. Plugins may extend the available options.
var modella = var validators = var User = ; User; User
Model.validate( fn )
Adds a validator to the model. A validator should add error messages for attributes that fail validation. Note that plugins such as modella/validators make extensive use of this.
var User = ; User;
Working with Instances
new Model( [attrs] )
You can create instances of models with the new
operator. You can also specify
initial values by passing in an object for attrs
.
var user = ; var bob = username: 'Bob' ;
Model#attribute(value)
Sets the given attribute to a value. Passing undefined
will unset
the attribute.
var user = ; user;
Model#attribute()
Returns the value of the attribute
var user = username: 'Bob'; user // => 'Bob'
Model#get( attr )
Returns the value of the attribute
var user = username: 'Bob'; user // => 'Bob'
Model#unset( attr )
Unset the attribute
var user = username: 'Bob' age: 27; user; // user.username() === undefined
Model#has( attr )
Returns whether an instance has an attribute set.
var user = username: 'Bob'; user // => false
Model#set( attrs )
Quickly sets multiple attributes. Using undefined
for a value will
unset the attribute.
var user = ; user;
Model#primary( [key] )
Gets or sets the value of the primary key
attribute. By default, this auto-maps to an
attribute with the name of _id
or id
if it is specified.
Getting the primary key:
var User = ;var user = _id: 123 ; user; // => 123
Setting the primary key:
var User = ;var user = _id: 123 ;user; user; // => 456
Model#isNew()
Returns a boolean based on if the value of Model#primary()
is set or not.
var user = ;user // => true var oldUser = _id: 555;oldUser // => false
Model.isValid()
Runs all validators on the model and returns whether any validations failed.
var User = var validators = ; User ; var user = ; user // => false
Model#save( [fn(err)] )
Saves the model using the syncLayer
. Will not attempt to save if
model#isValid()
returns false.
Calls fn(err)
after save.
var User = ; var user = name: 'Charley' ; user;
Using events:
user; user; user;
Model#remove( [fn(err)] )
Deletes the model using the sync layer and marks it as removed
.
Calls fn(err)
after remove.
Model#removed
Marked as true if the model has been deleted.
user; userremoved // => true
Model#model
Points to the base model from which the instance was created.
var user = ; usermodel === User // => true
Model#json()
Model#toJSON()
Gets the JSON representation of the model
Writing Plugins
Modella is made to be extended! Use events to hook into modella and manipulate the data as necessary. See below for the list of events.
For types of plugins, and more comprehensive documentation, see the plugin writing guide.
Here's some existing plugins to help you get started: list of available plugins.
Events
All modella models have built in emitters on both instances and the model itself.
You can listen for an event on either the instance
of a model or the Model
itself. Here's how to listen on the instance:
var user = user;
Listening on the Model
is useful for performing aggregate operations on all instances. Here's how to listen on the Model
:
User;
List of All Events
Save Events
save
triggers after a successful save.create
triggers after a record is saved for the first time.saving
triggers before saving has occurred.saving
requires asynchronous callbacks so that validation can occur.
Asynchronous callback:
user; User;
Remove Events
remove
: triggers after a successful removal.removing
: triggers before a remove has occurred.removing
must call an asynchronous callback so that execution can continue
Asynchronous callback:
user; User;
Validation Events
invalid
triggers whenisValid()
orvalidate()
fails.valid
triggers whenisValid()
orvalidate()
passes.
Manipulation Events
change <attr>
triggers whenattr
changes (viaset
ormodel.attr(val)
.
user username'charley';
initializing
triggers when a newinstance
is created. Passesattrs
which can be modified by the listener.
User; var bob = name: 'Bob';bobname // => BOB
setting
triggers wheninstance.set
is called. Passesattrs
which can be modified in the listener.
User;var bob = ;bob;bobname // => BOB
Miscellaneous Events
attr
triggers when a new attribute is added/changed (Model.attr(name, options)
)initialize
triggers when a model has been completely initialized.error
triggers whenever there's an error syncing the model.
License
(The MIT License)
Copyright (c) 2013 Ryan Schmukler ryan@slingingcode.com
Copyright (c) 2013 Matthew Mueller mattmuelle@gmail.com
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.