computed-observable

An approximate impementation of computed observables

npm install computed-observable
1 downloads in the last week
4 downloads in the last month

computed observable

npm install computed-observable

An approximate implementation of Knockout's computed observables.

Usage

  var CO = require('computed-observable');

A somewhat familiar interface

Note that you don't have to invoke this.firstName or this.lastName within the computed function. Just reference the variables naturally. The same goes for assignment. Notice that we use person.firstName = instead of invoking some setter function.

  function Person() {
    this.observable('firstName', 'Bobby');
    this.observable('lastName', 'Stevenson');
    this.computed('fullName', function() {
      return this.firstName + ' ' + this.lastName;
    });
  };

  var person = new CO(Person);

  person.on('changed', function() {
    console.log('Model changed', this.fullName);
  });

  person.firstName = 'Stanislaw';

An unfamiliar interface

You can give CO an initialization function, or an object containing properties.


var person = new CO({
  firstName: 'Bobby',
  lastName: 'Stevenson',
  fullName: function() {
    return this.firstName + ' ' + this.lastName;
  }
});

person.on('changed', function() {
  //this.fullName === 'Stanislaw Stevenson'
});

person.firstName = 'Stanislaw';

Here, it's assumed that function properties are meant to be dynamic (computed), while other attributes are meant to be observable. This reduces boilerplate significantly.

License

MIT

npm loves you