Skip to content
/ model Public

Watch an object and all of it's properties for changes

Notifications You must be signed in to change notification settings

ripplejs/model

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

model

Build Status

Watch an object and it's properties for changes, including nested properties.

Install

component install ripplejs/model

Example

var model = require('model');

// Creates a new model contructor
var ViewModel = model();

// Create a new view-model object with these properties
var model = new ViewModel{
  firstname: "Tom",
  lastname: "Dickson",
  items: [1, 2, 3],
  data: {
    height: '175cm'
  }
});

// Watch arrays for items added, removed or sorted
model.change('items', function(){
  console.log('array changed');
});

// Watch computed properties for changes
model.change('fullname', function(val){
  console.log(val);
});

// Watch nested properties for changes
model.change('data.height', function(){
  console.log('height changed');
});

// Values must be changed with #set
model.set('firstname', 'Richard');

// Get nested properties
model.get('data.height'); // '175cm'

// Get computed properties
model.get('fullname'); // 'Richard Dickson'

// Arrays can be used as normal
model.get('items').push(4); // 'array changed'

About

Watch an object and all of it's properties for changes

Resources

Stars

Watchers

Forks

Packages

No packages published