PDA

View Full Version : Creating Dynamic Classes with HTTP Datasets


cdickson
02-12-2003, 04:39 PM
Hi,

During the running of my application,
I am making an HTTP Request on a button click (code not shown) and getting back xml data similar to the following for my dataset:

<keyset>
<key value="bob"/>
<key value="joe"/>
<key value="mike"/>
</keyset>

With this dataset, i want to create a stack of text views displaying those names. The approach i was taking is shown below. However, this was not working. Is there a better approach?


<view name="MainView>

<datapointer name="keysetDI" xpath="keysDataset:/" ondata="MainView.someMethod()"/>

<method="someMethod">

var keysDP = MainView.keysetDI.getXPath('keyset');

Debug.write(keysDP.serialize()); // i have verified my dataset with this statement

var myClass = new MyClass( this, {datapath:keysDP} );

</method>

</view>

<library>
<class name="MyClass" height="40" bgcolor="yellow" font="Helvetica" fontsize="20" visible="true" >
<text name="key" width="300" datapath="key[-5]" ondata="this.setText( '@value' ) "/>
<simplelayout axis="y" spacing="10"/>
</class>
</library>


Thanks!

Clint Dickson Software Engineer
p 415.875.7061 f 415.875.7001 cdickson@semaphorepartners.com
.................................................. .................................................. ....................

Semaphore Partners www.semaphorepartners.com

antun
02-13-2003, 09:37 AM
Hey Clint

In your example, you used a datapointer. That's not really what they're for. A datapointer is a "cursor" that you use for looking at a dataset.

For the purpose of duplicating (replicating) views, you want datapaths. You don't need to instantiate the class you've defined using new, as you were trying to. Instead, you just give its parent a datapath (blue) of the parent node of the one you want replicated, and then give the view you want replicated a datapath of just the node you want to be duplicated (red). That probably sounds really confusing so hopefully this will clarify it ;-)


<canvas debug="true">
<dataset name="keysDataset">
<keyset>
<key value="bob"/>
<key value="joe"/>
<key value="mike"/>
</keyset>
</dataset>

<class name="MyClass" height="40" bgcolor="yellow"
fontsize="20" visible="true">
<text name="key" width="300" datapath="@value" />
</class>

<view name="MainView" datapath="keysDataset:/keyset">
<simplelayout axis="y" spacing="10"/>
<MyClass datapath="key" />
</view>
</canvas>


The <text> node above (green) will inherit the datapath from it's parent, so you can just use @value in it's datapath to set it.

BTW, I noticed you used the <library> tag in your example. I presume that you just cut and pasted from another file, because you don't use a library tag in a file where you have a canvas tag already, right?

-Antun

antun
02-13-2003, 10:22 AM
Note that if the data in a dataset changes, all replicated views will automatically be updated. In the example below, I'm actually changing the datapath using the setDatapath() method. This is kind of a hack to simulate a dynamic dataset.


<canvas debug="true">
<dataset name="keysDataset">
<keyset>
<key value="bob"/>
<key value="joe"/>
<key value="mike"/>
</keyset>
</dataset>

<dataset name="keysDatasetTwo">
<keyset>
<key value="smelly"/>
<key value="grumpy"/>
<key value="greedy"/>
<key value="sleepy"/>
<key value="happy"/>
</keyset>
</dataset>

<class name="MyClass" height="40" bgcolor="yellow"
fontsize="20" visible="true">
<text name="key" width="300" datapath="@value" />
</class>

<view name="MainView" datapath="keysDataset:/keyset">
<simplelayout axis="y" spacing="10"/>
<MyClass datapath="key" />
</view>

<button x="125" y="20"
onclick="MainView.setDataPath( 'keysDatasetTwo:/keyset' )">
Change Dataset
</button>
</canvas>


-Antun

cdickson
02-14-2003, 02:22 PM
How could i go about naming the view that is dynamically created. so lets say i keep a counter (called counterValue) and want to name the dynamic view "name" + counterValue.

I would like to do this so i could later on call the dynamic view to change its attribute like

MainView.name2.setVisible(false);


Thanks!

antun
02-14-2003, 02:45 PM
You can't really "name" the view as you do a regular view. What happens when a view is replicated based on a datapath is that it becomes a LzReplicationManager. You name the view that will get cloned, and you can then access the cloned views using its getCloneNumber() method:


<canvas>
<dataset name="keysDataset">
<keyset>
<key value="bob"/>
<key value="joe"/>
<key value="mike"/>
</keyset>
</dataset>

<class name="MyClass" height="40" bgcolor="yellow"
fontsize="20" visible="true">
<text name="key" width="300" datapath="@value" />
</class>

<view name="MainView" datapath="keysDataset:/keyset">
<simplelayout axis="y" spacing="10"/>
<MyClass name="myClone" datapath="key" />
</view>

<button x="125" y="20"
onclick="MainView.myClone.getCloneNumber(1).setVisible( false );">
Hide the second cloned view
</button>
</canvas>


Does that help?

Take care,

Antun