Created
July 10, 2012 21:56
-
-
Save aadsm/3086468 to your computer and use it in GitHub Desktop.
2-way data bindings in MontageJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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