Skip to content
This repository has been archived by the owner on May 15, 2023. It is now read-only.

Detecting Variable Changes

Stephen Oney edited this page Dec 29, 2013 · 8 revisions

###onChange When constraints affect some non-DOM property (e.g. RaphaelJS objects or SVG objects), a more general mechanism can be used. .onChange(callback), for instance, specifies to call callback whenever a constraint's value is invalidated (see the "Internals" section for a discussion on invalidation). callback can then perform any necessary updates.

var c = cjs.constraint(1);
c.onChange(function(new_val, old_val) {
    console.log("was :" + old_val +", now: " + new_val);
}); // was: null, now: 1

c.set(2); // was: 1, now: 2

.onChange(callback) hooks can be removed with the .offChange(callback) function.

###liven cjs.liven(func) automatically calls func whenever any constraints that func fetches are invalidated. For instance:

var x = cjs.constraint(0),
    y = cjs.constraint(0);
var live_fn = cjs.liven(function() {
    var x_val = x.get(),
        y_val = y.get();

    some_other_library.setPosition(x_val, y_val);
});

The above snippet will automatically call some_other_library.setPosition whenever x or y changes.


Next: Array & Map Constraints

Previous: DOM Bindings

Clone this wiki locally