antun
03-06-2003, 01:29 PM
You can constrain attributes to arbitrary variables in Laszlo, however it's important to be aware of the underlying events. In the example below, we're trying to accomplish something quite simple: constrain the visible attribute of a view, to an arbitrary attribute (myAttr) that we created in another view. When that other view is clicked, we alternate its value between 1 and -1 (red).
We constrain the visible attribute of the flashwer view to an equality test (green) which will either evaluate to true or false.
<canvas>
<simplelayout spacing="5" />
<text name="myButton" bgcolor="yellow" width="100" x="10"
onclick="this.myAttr *= -1;
this.setText( 'myAttr: ' + this.myAttr);">
<attribute name="myAttr" value="1"/>
Click Me!
</text>
<view name="flasher" bgcolor="blue" x="10">
<attribute name="visible" constraint="parent.myButton.myAttr == 1"/>
<text fgcolor="white">
This blue box should disappear/reappear
</text>
</view>
</canvas>
However this example is not going to work, because although the assignment of myAttr happens, we don't send out an event, so the visible attribute in flasher, who is listening for that event, doesn't hear about it. That's why in this case we have to use the setAttribute method:-
<canvas>
<simplelayout spacing="5" />
<text name="myButton" bgcolor="yellow" width="100" x="10"
onclick="this.setAttribute( 'myAttr', this.myAttr * -1 );
this.setText( 'myAttr: ' + this.myAttr);">
<attribute name="myAttr" value="1"/>
Click Me!
</text>
<view name="flasher" bgcolor="blue" x="10">
<attribute name="visible" constraint="parent.myButton.myAttr == 1"/>
<text fgcolor="white">
This blue box should disappear/reappear
</text>
</view>
</canvas>
Enjoy!
We constrain the visible attribute of the flashwer view to an equality test (green) which will either evaluate to true or false.
<canvas>
<simplelayout spacing="5" />
<text name="myButton" bgcolor="yellow" width="100" x="10"
onclick="this.myAttr *= -1;
this.setText( 'myAttr: ' + this.myAttr);">
<attribute name="myAttr" value="1"/>
Click Me!
</text>
<view name="flasher" bgcolor="blue" x="10">
<attribute name="visible" constraint="parent.myButton.myAttr == 1"/>
<text fgcolor="white">
This blue box should disappear/reappear
</text>
</view>
</canvas>
However this example is not going to work, because although the assignment of myAttr happens, we don't send out an event, so the visible attribute in flasher, who is listening for that event, doesn't hear about it. That's why in this case we have to use the setAttribute method:-
<canvas>
<simplelayout spacing="5" />
<text name="myButton" bgcolor="yellow" width="100" x="10"
onclick="this.setAttribute( 'myAttr', this.myAttr * -1 );
this.setText( 'myAttr: ' + this.myAttr);">
<attribute name="myAttr" value="1"/>
Click Me!
</text>
<view name="flasher" bgcolor="blue" x="10">
<attribute name="visible" constraint="parent.myButton.myAttr == 1"/>
<text fgcolor="white">
This blue box should disappear/reappear
</text>
</view>
</canvas>
Enjoy!