PDA

View Full Version : Array as attribute ?


3.14
05-04-2004, 10:15 AM
i'd like to have an 'array' as attribute of a class, for example a 'selectedItems' attribute which would contain a variable number of items
is it possible to make it without using datasets, maybe only with Javascript arrays ?
thx

antun
05-04-2004, 10:17 AM
Absolutely:


<class name="myspecialclass">
<method event="oninit">
this.foo = new Array();
</method>
</class>


As of LPS 2.1.1, you can't do <attribute value="new Array()" />, but there is a bug filed against that and it should be fixed in a future release.

-Antun

3.14
05-04-2004, 10:24 AM
but what happens when you add a value to the Array? does it broacast an event 'onfoo' that tells your variable 'foo' has been changed?

and is it possible to bind this attribute inside another component?

antun
05-04-2004, 10:46 AM
Changing an array variable does not appear to send an event. I've filed a bug/request for this. However, you can send your own event as follows:


<canvas debug="true">

<class name="myspecialclass">
<attribute name="foo" />
<method event="oninit">
this.foo = new Array();
</method>
<method event="onfoo">
Debug.write( "foo changed" );
</method>
</class>

<myspecialclass id="bar" />

<button>Append value to foo
<method event="onclick">
bar.foo.push( 'smelly' );
bar['onfoo'].sendEvent();
</method>
</button>

</canvas>


What you could do if you wanted to is write some methods in your class that acted upon the foo array, and sent out the onfoo event. While you might not get the shorthand constraint functionality (width="${...}), you should be able to write a constraint the more verbose way (using a delegate).

-Antun