Skip to content

Instantly share code, notes, and snippets.

@aadsm
Created July 10, 2012 21:56
Show Gist options
  • Select an option

  • Save aadsm/3086468 to your computer and use it in GitHub Desktop.

Select an option

Save aadsm/3086468 to your computer and use it in GitHub Desktop.
2-way data bindings in MontageJS
var foo = {c: 3};
var bar = {a: {b: {c: 3}}};
// define a binding between foo.c and bar.a.b.c, so when we change the
// value of any of the two paths the new value will be copied to the
// other one.
Object.defineBinding(foo, "c", {
boundObject: bar,
boundObjectPropertyPath: "a.b.c"
});
console.log(foo.c); // outputs 3
bar.a.b.c = 6;
console.log(foo.c); // outputs 6
bar.a.b = {c: 12};
console.log(foo.c); // outputs 12
bar.a = {b: {c: 24}};
console.log(foo.c); // outputs 24
foo.c = 42;
console.log(bar.a.b.c); // since this is a 2-way binding, it outputs 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment