Base Model Class
App-model is the base class that adds some basic methods for dealing with models. It is created using uberproto and can be extended normally.
var Model = require( 'app-model' );
var exModel = Model.extend({
init: function( props ) {
this._super( props );
// Additional initialisation
}
});
exModel.add({
foo: 'foo'
});
@props object
attributes to add
Each property of the object passed in is added as a member on the model. Internally this uses Object.defineProperty to watch the properties.
This function also emits an add
event passing the value of the added property.
exModel.foo = 'oof';
Properties are added directly to the Model
object so changing data is trivial.
exModel.on( 'change:foo', function( value ) {
console.log( 'foo is changing to', value );
});
exModel.foo = 'bar';
-> 'foo is changing to bar'
Each instance of a Model
is an instance of an EventEmitter and will emit events when things change, allowing you to be data-driven.
add
Fired when a property is added to the model.
change
Fired whenever a property is reassigned.