This example shows how connect can be used.
The function connect() is defined in the lively.bindings module. In order to use it, you must ensure that the module is loaded. It currently is by default.
connect() takes care of pushing changes from one object to another when state is changed. Example:
obj1 = {x: 'foo'} obj2 = {y: '123'} connect(obj1, 'x', obj2, 'y') obj1.x foo obj2.y 123 obj1.x = 'bar' obj2.y bar
Here we connected the property 'x' of obj1 with the property 'y' of obj2. As obj1's value was changed, the change was propageted to obj2.y. The source slot must always be an attribute, the target slot can be an attribute or a method.
connect() is made persistent when the object is serialized.
There is an optional fifth parameter for connect that is a JS object with the optional properties converter, updater and removeAfterUpdate. When the converter property exists it should be a function that gets the value from the sourceObj as input and returns a value that is used for updating the target.
Example:
obj1 = {x: 'foo'} obj2 = {y: '123'} connect(obj1, 'x', obj2, 'y', {converter: function(val) { return val % 7 }}) obj1.x = 10 obj2.y 3
connect() currently stops on recursive activation. If you need recursion support please make a feature request.
