PDA

View Full Version : Exploring the New Attribute Syntax


antun
06-24-2003, 02:41 PM
If you've been using the LPS since before v1 was released, you may be familiar with the verbose way of declaring attributes using the <attribute> tag. This is frequently used for custom constraint types, setting attribute values at init time and so forth, in addition to defining custom attributes in class definitions.

The v1 release provides more powerful and user-friendly syntax for accomplishing some of this:


<text name="foo" label="${'My x position is ' + this.x}" />


You can put any JavaScript expression in between the curly braces, and it will be evaluated. The syntax above also constrains the attribute to the expression, so if you move the text field (try using the debugger for this) the text field will be updated.

Writing ${} is short for $always{} - the "always" referring to the constraint nature of the attribute. Alternatively, if you needed an attribute to be evaluated once, you can use the $once{} syntax:


<view name="foo" width="200" height="30" bgcolor="red">
<view width="$once{parent.width/2}" height="30" bgcolor="blue" />
</view>


In this case, if foo's width is changed (again, try using the debugger here), the blue view's width will remain constant. This is a replacement for the old syntax:


<attribute name="width" init="parent.width/2" type="number" />


This does not mean that the attribute tag is now useless - it is still required for defining custom attributes in class definitions, as well as verbosely declaring attributes for code clarity. In a class definition, you can now use the when attribute of the attribute tag, to correspond with the above described behaviour:


<class name="myClass">
<attribute name="myInitOffset" value="this.x" type="number"
when="once" />
<attribute name="halfMyWidth" value="this.width/2"
type="number" when="always" />
</class>


Enjoy!

fortetie
02-01-2006, 05:44 AM
Hello,
I would like to express something like:
visible="${classroot.attr1 && classroot.attr2}"

The && caracters are obviously forbidden... what is the correct syntax? Where can I find a description of what can be done with the constraint syntax?

Thanks,
Etienne

antun
02-01-2006, 06:25 AM
LZX is XML, so you have to escape the ampersand character. Use &amp;amp; instead.

-Antun

spoco2
04-19-2006, 04:49 PM
I've asked this in the coding forum as well, but is quite relevent to this tip... Is is possible set an attribute's value as a constraint by pulling the constraint definition from a dataset?

By way of example:

I have used this following code to create views on the fly which have an attribute correctly constrained:
kids.push({ name: "LzView",
attrs: { name: "somename",
x: ${someview.width}",
y: 0}});

this.createChildren(kids);

The x value of the 'somename' view is correctly constrained to the someview.width.

If, however I want to store all the view definitions in a database and read it all at runtime to recreate a previously saved state (as I do now), if try to do the same thing, but create the 'kids' to create via code like:
(parent.objects is a Datapointer... this code is run for each node in the dataset, whith each node representing a view objname=classname to create)

do{
attarr = new Array();
attarr = parent.objects.getNodeAttributes();

objarr.push({
name:parent.objects.getNodeAttribute('objname'),
attrs: attarr}
);
}while(parent.objects.selectNext());
parent.createChildren(objarr);


Now in this version, even though my dataset has things set like:
<object objname="Structure" name="structurewindow" height="${this.parent.height}" width="${this.parent.structuredivslider.x}" x="0" y="0"/>
The height and width variables are not set correctly... they end up with that text in them, rather than evaluating to become the constraint I want them to be... Using values such as the x and y in this example works fine.

Any ideas on how to have laszlo treat the constraint strings as a constraint rather than just text?

Thanks in advance,

Simon

Gnome
12-04-2006, 03:13 PM
the getNodeAttributes() method returns an object type with a listing of the attribute for the current node. The question that needs to be answered to fulfill the problem at hand, is how to transverse the object into an array of attibute name/value pairs.

I have run into this same problem.

Does anyone know how to get the data from the object into an array to exploit the name/value pairs. this sort of dynamic functionality, IMHO, is what makes Laszlo a fantastic tool.

It could be used to parse out different data elements without having to explicitly set the datapath on the elements receiving the data. An example might be user settings at login time. Yet, I could use the same parser by dynamically setting the datapath and pointer and retrieving the new data to be applied to the new object derived from the tag that represents the name of a class.

Does this make sense?

Bill