Skip to content

treihadwyl/app-model

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Releases

No releases published

Packages

No packages published