Extensible observable models.
obs-model provides a minimal yet very extensible baseline for defining MVC or MVVM models with observable attributes.
For an example of extending models with plugins, see the validation plugin.
npm install obs
git clone https://github.com/pluma/obs-model.gitcd obs-modelnpm installmake && make dist
component install pluma/obs-model
Download the latest minified CommonJS release and add it to your project.
Download the latest minified AMD release and add it to your project.
var model = require'obs-model';var User = model'User'attr'id'attr'username'attr'firstname'attr'lastname'attr'fullname'return thisfirstname + ' ' + thislastname;var tokens = valuesplit' ';thisfirstnametokensshift;thislastnametokensjoin' ';watch: 'firstname' 'lastname';var john = id: 1 username: 'jdoe' firstname: 'John' lastname: 'Doe';console.logjohnfullname; // "John Doe"johnlastname'Smith';console.logjohnfullname; // "John Smith"johnfullname'John von Johnson';console.logjohnlastname; // "von Johnson"johnusernamesubscribeconsole.log"john's username is now: " + value;;johnusername'admin';// -> "john's username is now: admin"
Model with the given name.
The model's name as passed to
Defines a new attribute with the given name. The
options can be an object containing configuration data for the attribute.
The attribute will appear as an observable property on new model instances.
If either of these options is given, a computed observable with its context bound to the model will be created instead of a regular observable property.
options.read is given,
options.watch may optionally be an array of attribute names the computed observable will monitor for changes.
options.read is given,
options.lazy may optionally be a boolean determining whether the computed observable will use lazy evaluation.
Adds the given plugin to the model. The function will later be called by the model's constructor with
this set to the new model instance. If you add the same plugin multiple times, it will only be added once.
This method is the backbone of model extensibility.
If this function exists, it will be applied to the
Model definition when the plugin is added.
This function can provide additional properties or behaviour to the
Model type itself.
Creates a new model instance.
values is optionally an object mapping attribute names to their initial values.
Model this model is an instance of.
An observable property representing each attribute.
Runs the model's destructors. Plugins can use these to free event listeners, subscriptions and such.