PDA

View Full Version : Adding datapath support to simpleinputtext


sfarrow
06-26-2003, 06:51 AM
Greetings forum,

What would need to be done to <simpleinputtext> to accommodate a datapath? Can the datapath specified on the class be forwarded to the embedded <inputtext>.

Thanks.

antun
06-26-2003, 09:48 AM
Hey sfarrow

Well, I tried using simpleinputtext and it didn't work. I'm not sure why. Instead I ended up writing my own "mysimpleinputtext" class that did much the same thing (mine had a border instead):


<canvas debug="true">
<include href="lz/simpletext.lzx" />

<dataset name="myDS">
<foo bar="smelly" />
</dataset>


<class name="mysimpleinputtext" bgcolor="0x000000" width="200"
height="16">

<method name="retrieveData">
return this.whitecontents.thefield.getText();
</method>

<view name="whitecontents" width="parent.width-2"
height="parent.height-2" align="center" valign="middle"
bgcolor="white">
<inputtext name="thefield" width="parent.width-2"
text="${parent.parent.data}">
<method event="ontext">
parent.parent.datapath.retrieveData();
</method>
</inputtext>
</view>
</class>

<mysimpleinputtext name="myField" datapath="myDS:/foo/@bar" />

</canvas>


You can use this to create any kind of text field you want. There are two things going on here:

text="${parent.parent.data}"

The text attribute (the contents of the text field) of the inputtext is bound the the data attribute of the datapath. This sets the text initially, and updates it if the data in the dataset changes.


<method event="ontext">
parent.parent.datapath.retrieveData();
</method>


The retrieveData() is magic, in that the return value is what gets sent back to the dataset. It still needs to be called (here we're calling it every time there is a change to the text field). In reality, you may want to call it when the user clicks a button or something.

You can test this by opening the debugger, and typing:


canvas.datasets.myDS.getPointer().serialize()


... to write out the data that's in the dataset.

-Antun

sfarrow
06-27-2003, 04:46 AM
Thanks Antun. Precisely what I was after.

Not a pressing requirement, but just curious how the class could be extended to accommodate either a datapath *or* a text attribute as does inputtext.

antun
06-30-2003, 09:00 AM
Hey sfarrow

Something like this:


<canvas debug="true">
<include href="lz/simpletext.lzx" />

<dataset name="myDS">
<foo bar="smelly" />
</dataset>


<class name="mysimpleinputtext" bgcolor="0x000000" width="200"
height="16">
<!-- Attributes -->
<attribute name="text" type="string" value="" />

<!-- Methods -->
<method event="oninit">
if ( !this.datapath )
this.whitecontents.thefield.setText( this.text );
</method>

<method name="retrieveData">
return this.whitecontents.thefield.getText();
</method>

<method name="getText">
return this.whitecontents.thefield.getText();
</method>

<method name="setText" args="s">
this.whitecontents.thefield.setText( s );
</method>

<!-- Views -->
<view name="whitecontents" width="parent.width-2"
height="parent.height-2" align="center" valign="middle"
bgcolor="white">
<inputtext name="thefield" width="parent.width-2"
text="${parent.parent.data}">
<method event="ontext">
if ( !parent.parent.text )
parent.parent.datapath.retrieveData();
</method>
</inputtext>
</view>
</class>

<simplelayout spacing="10" />
<mysimpleinputtext name="myField" datapath="myDS:/foo/@bar" />
<mysimpleinputtext name="myField" text="FooBar" />

</canvas>


You'd still get a bunch of errors in the debugger, which are annoying, and will affect performance while debugging, but shouldn't be a problem otherwise.

I've added a couple of if blocks to minimize this effect, by checking if the text attribute is present before trying to bind stuff to data.

The right approach would be to use the applyData() method in place of this constraint:


text="${parent.parent.data}"


I've also added getText() and setText() methods so that it behaves more like a regular text field.

-Antun