yoDon
09-02-2004, 03:56 PM
I spent most of the afternoon figuring out how to use setters to initialize member objects.
In case anyone else is interested, here's what I came up with:
<class
name="SampleSetterClass"
extends="view">
<attribute name="Label" type="string" value="...loading..." setter="_setLabel(Label)"/>
<attribute name="Value" type="string" value="...loading..." setter="_setValue(Value)"/>
<attribute name="_initialLabel" type="string"/>
<attribute name="_initialValue" type="string"/>
<method name="_setLabel" args="t">
if ( typeof(this.implLabel)!="undefined" )
{ this.implLabel.setText( t ); }
else
{ this._initialLabel = t; }
</method>
<method name="_setValue" args="t">
if ( typeof(this.implValue)!="undefined" )
{ this.implValue.setText( t ); }
else
{ this._initialValue = t; }
</method>
<method name="init">
super.init();
if ( typeof(this._initialLabel)!="undefined" )
{ this._setLabel( this._initialLabel ); }
if ( typeof(this._initialValue)!="undefined" )
{ this._setValue( this._initialValue ); }
</method>
<simplelayout axis='x'/>
<text name="implLabel" width="120"/>
<text name="implValue" />
</class>
The class creates a composite object (a view containing two text objects) where the text properties of the child objects can be initialized and changed by setting attributes on the parent object.
An example usage for the class is
<view>
<simplelayout axis='y'/>
<SampleSetterClass name="Foo" Label="First" Value="good"/>
<SampleSetterClass name="Bar" Label="Second" Value="bad"/>
</view>
//Note: after the objects are created, the
// displayed text can be manipulated
// via commands like Foo.Label="Third"
The challenge was that class attributes are created and initialized before class member objects are constructed, so the initial setter call needs to temporarily stash the value on a dummy attribute until it can be recovered once the object is fully initialized (and all member objects have been constructed).
(WEB-INF/lps/components/lz/edittext.lzx was my starting place on this, but it made use of a _initcomplete attribute only found on components derived from text. The view class didn't seem to provide an _initcomplete attr, so I decided I'd build my classes using ( typeof(member)!="undefined" ) since that approach should work regardless of the base class.)
enjoy,
-Don
In case anyone else is interested, here's what I came up with:
<class
name="SampleSetterClass"
extends="view">
<attribute name="Label" type="string" value="...loading..." setter="_setLabel(Label)"/>
<attribute name="Value" type="string" value="...loading..." setter="_setValue(Value)"/>
<attribute name="_initialLabel" type="string"/>
<attribute name="_initialValue" type="string"/>
<method name="_setLabel" args="t">
if ( typeof(this.implLabel)!="undefined" )
{ this.implLabel.setText( t ); }
else
{ this._initialLabel = t; }
</method>
<method name="_setValue" args="t">
if ( typeof(this.implValue)!="undefined" )
{ this.implValue.setText( t ); }
else
{ this._initialValue = t; }
</method>
<method name="init">
super.init();
if ( typeof(this._initialLabel)!="undefined" )
{ this._setLabel( this._initialLabel ); }
if ( typeof(this._initialValue)!="undefined" )
{ this._setValue( this._initialValue ); }
</method>
<simplelayout axis='x'/>
<text name="implLabel" width="120"/>
<text name="implValue" />
</class>
The class creates a composite object (a view containing two text objects) where the text properties of the child objects can be initialized and changed by setting attributes on the parent object.
An example usage for the class is
<view>
<simplelayout axis='y'/>
<SampleSetterClass name="Foo" Label="First" Value="good"/>
<SampleSetterClass name="Bar" Label="Second" Value="bad"/>
</view>
//Note: after the objects are created, the
// displayed text can be manipulated
// via commands like Foo.Label="Third"
The challenge was that class attributes are created and initialized before class member objects are constructed, so the initial setter call needs to temporarily stash the value on a dummy attribute until it can be recovered once the object is fully initialized (and all member objects have been constructed).
(WEB-INF/lps/components/lz/edittext.lzx was my starting place on this, but it made use of a _initcomplete attribute only found on components derived from text. The view class didn't seem to provide an _initcomplete attr, so I decided I'd build my classes using ( typeof(member)!="undefined" ) since that approach should work regardless of the base class.)
enjoy,
-Don