classifyjs-observer

Classify-Ovserver is a mutator for Classify.js[https://github.com/weikinhuang/Classify] that allows for simple getters and setters, and on value change events listeners for object properties.

npm install classifyjs-observer
1 downloads in the last week
4 downloads in the last month

Classify-Observer.js

Classify-Ovserver is a mutator for Classify.js[https://github.com/weikinhuang/Classify] that allows for simple getters and setters, and on value change events listeners for object properties.

Classify-Observer is tested in IE 6+, Firefox 2+, Safari 3+, Chrome 3+, and Opera 10+, NodeJs.

Usage

Defining a observable property

var test = Classify({
    __observable_x : 10,
    __observable_y : {
        value : 100,
        get : function(value) {
            return value * 10;
        },
        set : function(value, original) {
            return this.a();
        }
    }
});
var instance = new test();

// test that observers are created
instance.x instanceof Classify.Observer;

// Calling observer from object context.
instance.x.get() === 10;
// Calling observer from object context with defined getter.
instance.y.get() === 1000;
// The return value of setter function is a chain of parent instance.
instance.y.set(10) === instance;
// Calling observer from object context with setter referencing parent function.
instance.y.get() === 10;

// add an event listener
instance.y.addListener(function(value) {
    console.log(value);
});
// event fired
instance.y.set(1);

// test separate instances of instantiated class
var instance2 = new test();
instance.x !== instance2.x; //"Separate instances of classes have different observers.";

Environments

Classify is CommonJS compliant and can be used in the browser scope or the server scope.

In the browser:

<script src="path/to/classify.js" type="text/javascript"></script>
<script src="path/to/classify-observer.js" type="text/javascript"></script>
<!-- Classify() is now in the global context -->

In NodeJs environment:

npm install classifyjs
npm install classifyjs-observer
var Classify = require("classifyjs").Classify;
// only needed once where classifyjs is first required
require("classifyjs-observable");

With an AMD loader like RequireJS:

require({
    "paths" : {
        "classify-observer" : "path/to/classify-observer"
    }
}, [ "classify-observer" ], function(Classify) {
    console.log(Classify.Observer);
});

Building the Source

Classify-Observer uses the grunt build system. Building Classify-Observer requires node.js and a command line gzip program.

# Install grunt.
$ npm install -g grunt-cli

# Clone the Classify-Observer git repo.
$ git clone git://github.com/weikinhuang/Classify-Observer.git
$ cd Classify-Observer
$ git submodule update --init

# Install node modules.
$ npm install

# Run grunt.
$ grunt

Running the tests:

$ grunt test

There are many other tasks that can be run through grunt. For a list of all tasks:

$ grunt --help

Changelog

v0.11.0

Initial split from core Classify[https://github.com/weikinhuang/Classify] mutator

About

Classify copyright 2011-2013 by Wei Kin Huang.

Build Tools: Grunt, QUnit, Benchmark.js, UglifyJS, JsHint, JsCoverage.

All code released under the MIT License.

Fork me to show support and help fix bugs!

npm loves you