PDA

View Full Version : sharing variable between different views


chocolat
03-19-2009, 12:03 AM
hi,

My application hierarchy :


<canvas>

<view name="one">
<....>
<view name="two" >
<method event="onclick">
variable1=someText;
</method>
</view>
</....>
</view>

<view name="three">
<....>
<view name="four" >
<handler name="oninit">
<![CDATA[
//this does'nt work
variable2 = vairiable1;
]]>
</view>
</....>
</view>

</canvas>


I don't know how to get the text from the view "two", in the view "four" :confused: ...
thanks,

chocolat

julien.lzx
03-19-2009, 06:02 AM
hello Chocolat,

A solution could be to use constraints :

<canvas debug="true">
<script>
/*1*/ var someText = "here is some text for view 2 and 4";
</script>

<view name="one">
<view>
<view>
<view>
<view id="viewTwo" name="two" width="200" height="100" bgcolor="#eeddff" >
<!--2--> <attribute name="variable1" type="string" value=""/>
<handler name="onclick">
/*3*/ this.setAttribute("variable1", someText);
Debug.write(this.variable1 );
/*4*/ viewFour.setAttribute("variable2",this.variable1);
</handler>
<text text="${parent.variable1}"/>
</view>
</view>
</view>
</view>
</view>

<view name="three">
<view>
<view>
<view>
<view id="viewFour" name="four" width="200" height="100" bgcolor="#ffddee" x="400">
<!--5--> <attribute name="variable2" type="string" value="${viewTwo.variable1}"/>
<text text="${parent.variable2}"/>
<!--other possibility:-->
<text text="${viewTwo.variable1}" y="20"/>
</view>
</view>
</view>
</view>
</view>

</canvas>


Hope this helps,
best regards,
Julien

pugmaster
03-19-2009, 09:04 AM
Variables (node or view names) are arranged in a hierarchical tree in Laszlo, where the canvas is the topmost node. There are two ways to access variables through this tree; through absolute or relative addressing. Absolute addressing begins with the canvas, while relative addressing uses the parent keyword to move upward through the tree.

We'll update your code with examples of both absolute and relative (we'll ignore the ...)


<canvas>

<view name="one">
<....>
<view name="two" >
<method event="onclick">
variable1=someText;
</method>
</view>
</....>
</view>

<view name="three">
<....>
<view name="four" >
<handler name="oninit">
<![CDATA[
//this does'nt work
variable2 = canvas.one.two.variable1; // absolute
variable2 = parent.parent.one.two.variable1; // relative
]]>
</view>
</....>
</view>

</canvas>


- Norman Klein
Author: Laszlo in Action

chocolat
03-20-2009, 01:26 AM
Thanks you two

I used Norman's relative path method
It works but the code looks "heavy" :
parent.parent.parent.parent.parent.parent.parent.p arent.view.view.view.view.view.variable

Julien's method may work to have a "prettier" code, but since my app works now, i will come back on this variable "issue" afterwards.
But i doubt that we can access viewTwo.variable1 inside viewFour in your code : <text text="${viewTwo.variable1}" />

chocolat

bastien
03-20-2009, 06:01 AM
Hi,

Since variable1 and variable2 are both used in the two views, there are no reason they should belong to the views separatly. I would go and put common variable in a node that can be seen as a structure.

The advantage is that you can pass the node to a method that can play with the variables.


<canvas>
<!-- the node that contains all my common variables !-->
<node id="common_var">
<attribute name="var1" type="string" value=""/>
<attribute name="var2" type="string" value=""/>
</node>

<simplelayout axis="y"/>

<view>
<!-- first view -->
<simplelayout axis="x"/>
<text text="${common_var.var1}" resize="true"/>
<text text="${common_var.var2}" resize="true"/>
</view>


<view>
<!-- second view -->
<simplelayout axis="x"/>
<text text="${common_var.var1}" resize="true"/>
<text text="${common_var.var2}" resize="true"/>
</view>



<button text="English">
<handler name="onclick">
common_var.setAttribute('var1', "one");
common_var.setAttribute('var2', "two");
</handler>
</button>

<button text="French">
<handler name="onclick">
common_var.setAttribute('var1', "un");
common_var.setAttribute('var2', "deux");
</handler>
</button>


</canvas>


-- Bastien