PDA

View Full Version : Copying nodes across datasets using a datapointer


antun
05-15-2003, 02:53 PM
You may have heard mention of datapointers on the forums and in the tutorials and they are frequently glossed over. Here's a good example of what you'd use a datapointer for.

Say you have two datasets and you need to copy a particular node from one to another. In this case, we have two datasets of cartoon characters: the first is dedicated to ducks, while the second contains various animals. Hewey (a duck) must be copied from the second dataset to the first:


<canvas debug="true">
<!-- Data -->
<dataset name="first">
<ducks>
<character name="Donald" type="duck" />
<character name="Daisy" type="duck" />
<character name="Scrooge" type="duck" />
</ducks>
</dataset>

<dataset name="second">
<various_characters>
<character name="Mickey" type="mouse" />
<character name="Pluto" type="dog" />
<character name="Hewey" type="duck" />
<character name="Minnie" type="mouse" />
</various_characters>
</dataset>

<!-- Views -->
<simplelayout axis="y" spacing="2" />
<view name="characters" datapath="first:/ducks"
x="10" y="10">
<simplelayout axis="y" spacing="2" />
<view name="row" datapath="character" bgcolor="0xeaeaea">
<simplelayout axis="x" spacing="2" />
<text datapath="@name" />
<text datapath="@type" />
</view>
</view>

<button>Copy node over
<method event="onclick">
var dsOne = canvas.datasets.first;
var dsTwo = canvas.datasets.second;
var dpInFirst = dsOne.getPointer();
var dpInSecond = dsTwo.getPointer();
dpInFirst.setXPath( 'ducks' );
dpInSecond.setXPath( 'various_characters/character[3]' );
dpInFirst.addNodeFromPointer( dpInSecond );
</method>
</button>
</canvas>


Here we created two datapointers:

[list=1]
The first we pointed at the root node <ducks> in the first dataset. This is where we are copying to.
The second we pointed at the node we wanted to copy from, in this case the third <character> node in the second dataset. In this case we knew the offset (3) of the node, but we could have used a loop to search for all <character> nodes with a type attribute of "duck".
[/list=1]

Then we used the datapointer's addNodeFromPointer() method on the first datapointer, giving it an argument of the second pointer.

Enjoy!



Reference entry for addNodeFromPointer():
http://www.laszlosystems.com/developers/learn/documentation/lzxref/datapointer.php#meth-addNodeFromPointer

satyak
05-03-2005, 07:47 AM
I am just getting up to speed with Laszlo. I am trying to copy nodes from one data set to the other. Looks like I could do this using two schemes.

One is by using data pointers as you have pointed out. The other is through the data node's "insertbefore" method.

In the InsertBefore approach, it seem to operate on the data set directly, unlike a datapointer (which is a pointer to the innards of a dataset).

What is the implication of using one vs the other when views are holding on to datapaths. Will a datapath reflect the changes that are done to an underlying dataset via the "insertBefore" approach?

Also are there any utility functions that look like

copyDataSet(ds1, ds2)
copyDataSet(ds1XPath, ds2XPath)

Perhaps one could write one, I am trying to find out if there is a hidden treasure somewhere I might have missed.

Also from a datapath, there doesn't seem to be a way to get to its underlying data set node reference. I know I could get the root dataset from a data pointer. Or am I missing something?