Skip to content

Latest commit

 

History

History
65 lines (40 loc) · 1.41 KB

File metadata and controls

65 lines (40 loc) · 1.41 KB

Treihadwyl Base Model Class

Base Model Class

Installation

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
  }
});

Modelling Data

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.

Changing Data

exModel.foo = 'oof';

Properties are added directly to the Model object so changing data is trivial.

Events

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.