Tipe: a very simple model library
Tipe is an experiment in building a very simple library for building javascript
business models.
Installation
You can install Tipe with NPM:
% npm install tipe-model
Then you can require
it in your code:
var Tipe = require('tipe');
Documentation
See the annotated source code.
Usage
You can define models by extending Tipe.Model
and defining your model schema
in a callback function:
var Person = Tipe.Model.extend(function(attr) {
attr.accessor('firstName');
attr.accessor('lastName');
attr.property('fullName', {
serialize: false,
get: function() {
return this.firstName + ' ' + this.lastName;
}
});
attr.accessor('age', {
default: 20,
serialize: function(val) {
return val * 2;
},
set: function(val) {
this.attributes.age = parseInt(val);
}
});
});
This Person
model has four attributes:
firstName
lastName
fullName
(read-only, computed based on firstName
and lastName
and not
included in any JSON representation)age
(defaults to 20, parsed to integer when set and doubled in JSON
representation)
You can use it as follows:
var john = new Person({ firstName: 'John' });
john.lastName = 'Cleese';
console.log(john.firstName);
console.log(john.fullName);
console.log(john.age);
console.log(john.toJSON());
Credits
Author: Arjan van der Gaag
Email: arjan@arjanvandergaag.nl
URL: http://github.com/avdgaag/tipe
Released under a MIT license. See LICENSE for more information.