View Full Version : init called 4 times for 2 views
metasarah
03-04-2003, 07:01 AM
So I have two views and a simple class that defines an init method:
<class name="mview" extends="view" >
<method event="oninit">
Debug.write(this.name + ": " + this.subviews.length);
</method>
</class>
<mview name="root" datapath="mydata:/reading_list">
<simplelayout/>
<text name="foo">these are some books I like</text>
<mview name="booklist_view"
datapath="book" >
<text name="book_text" datapath="title/text()"/>
</mview>
</mview>
Debug windows shows:
: 1
: 1
booklist_view: 1
root: 4
I expected the last two lines, but not the first two.
I also expected the booklist_view to have 3 subviews (because there are 3 books in the list) and for "root" to have only one subview, which is the booklist_view.
Seeking enlightenment,
Sarah
antun
03-04-2003, 07:31 AM
Hey Sarah
When I ran this code:-
<canvas debug="true">
<class name="mview" extends="view" >
<method event="oninit">
Debug.write(this.name + ": " + this.subviews.length);
</method>
</class>
<mview name="root" datapath="mydata:/reading_list">
<simplelayout/>
<text name="foo">these are some books I like</text>
<mview name="booklist_view" datapath="book" >
<text name="book_text" datapath="title/text()"/>
</mview>
</mview>
</canvas>
Which is basically what you posted in the body of your email, I got:-
booklist_view: 1
root: 2
In the debugger. Are you sure you're getting the extra lines in there?
-Antun
I can see why this is confusing. In fact, you're seeing oninit being called for each of your interior views that is replicated.
The problem is that replicated views can't have a name (since they're not unique).
Here's an explanation of the debugger messages:
booklist_view: 1
>> First booklist_view inits. It shouldn't be named, since it was replicated. Let's call that a bug. :rolleyes:
oninit_subviews.lzx:25: reference to undefined property 'name'
>> Second booklist_view evaluates its Debug message when it inits. It doesn't have a name since it's replicated, so before it writes to the debugger, it generates this warning.
: 1
>> Second (replicated) booklist_view inits.
: 1
>> Third (replicated) booklist_view inits. It doesn't generate a warning since you only see each warning once.
root: 4
>> Outside (root) view inits.
By the way, if a node replicates, the thing that takes over the named spot in the parent is called a ReplicationManager -- you'll find this in the docs. You can address a specific replicated view using the replication manager's getCloneNumber() method. For instance typing this into the debugger:
root.booklist_view.getCloneNumber(1).setX( 55 )
should move the middle book over a little bit.
P.S: That's quite a reading list!!
antun
03-04-2003, 08:39 AM
When I tried Sarah's code, I used the stuff from the body of her message, instead of adding a debug statement to the attachment.
Good answer by the way Adam.
-Antun
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.