PDA

View Full Version : parent-child relationship


dh_harshal
04-15-2009, 10:24 PM
Hi...

I have written a class which extends 'window'.
In that class I have a method which creates another instance of this class.
Now i want to assign, calling instance as parent as called instance as child.
Or simply maintain parent-child relationship across these instances.

How can I maintain parent(or its id) in the scope of called(newly created) instance of this class? I want to position these instances according to position of their parent. If position of parent changes, child should also move.


Code snippet is as follows -

<canvas>

<script>
var c = new lz.myclass(canvas, {name:......});
</script>

<class name="myclass" extends="window" height=....>
<attribute ...../>
.....
.....
<view resource="button.png" onclick=''this.parent.addNew()"/>
.....
<method name="addnew">
var c = new lz.myclass(canvas, {title:.....});
</method>
</class>

</canvas>



Now, my instances of this class should be positioned according to their parent(instance on which this new instance is created) position.
Also, I want to maintain certain space between 2 any two instances, so layout help is also needed.

Thanks in advance !!

bastien
04-16-2009, 01:13 AM
Hi,

Everything you need is in the first parameter of the class instanciation that indicates the parent of this new instance.

new lz.myclass(<parent>,{name: ...});

So, in your addnew method, as far as I understood what you want to do, replace the first parameter 'canvas', by 'this' and that should do the trick.

In attachment, a example of this.

Bastien

dh_harshal
04-19-2009, 10:50 PM
Thanks Bastien,

But in the example u gave, it was an inherent parent-child relationship.
i.e. children are placed inside parent view.

What I want to achieve is -
Suppose I have a window on canvas, and I create a new window which should be placed, say around 50px below my old window. Also, I want to assign 'parent=old window' for new window and whenever parent window moves, child window also should move to be at 50 px below parent window.

In ur example, if I assign 'this' in the constructor of new window, this new window goes inside old window, which is not desired.

Consider a tree design I want to achieve.
And moving one tree node, should result in stretching that adjoining line between nodes or nodes themselves.

Plzz, help.

mjessup
04-20-2009, 03:43 AM
Hi, If I am interpreting your question correctly I believe what you want to do is first hold your "parent" (which you seem to be using in a non-traditional sense) window as a reference, and then use a delegate to reposition it. Something along the lines of the following:


<canvas>
<window id="leaderWindow">
<simplelayout axis="y" spacing="5" />
<text text="leader window" />
<button text="Make Window">
<handler name="onclick"><![CDATA[
var win = this.parent
new lz.FollowerWindow(win.parent,
{'leaderWindow': this.parent,
'relx': win.width,
'rely': win.height}
);
]]></handler>
</button>
</window>
<class name="FollowerWindow" extends="window" allowdrag="false">
<attribute name="leaderWindow" type="expression" />
<attribute name="relx" type="number" />
<attribute name="rely" type="number" />
<handler name="oninit"><![CDATA[
var del = new LzDelegate(this, 'updatePosition');
del.register(this.leaderWindow, 'onx');
del.register(this.leaderWindow, 'ony');
updatePosition();
]]></handler>
<method name="updatePosition" args="ignore=null">
this.setAttribute('x', this.leaderWindow.x+relx);
this.setAttribute('y', this.leaderWindow.y+rely);
</method>
<text text="follower window" />
</class>
</canvas>

This code is assuming you would want the two windows to share the same parent/coordinate system (in this case the canvas). Otherwise you would need to tweak the delegate method (updatePosition) to make appropriate translations for the coordinate systems in which the views are living. You should be able to use a similar concept for any type of view, not just windows as in the example above.