computed-observable

0.0.1 • Public • Published

#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

Readme

Keywords

none

Package Sidebar

Install

npm i computed-observable

Weekly Downloads

1

Version

0.0.1

License

none

Last publish

Collaborators

  • Weltschmerz