On 9 December 2010 09:27, T.J. Crowder <[email protected]> wrote:
> Hi,
>
> The problem is that your `childs` property is shared by all of your
> instances, it's not specific to each instance. You have the same
> problem that the OP has in this question from just a few days ago, so
> see that thread for the details of why:
> http://groups.google.com/group/prototype-scriptaculous/browse_thread/thread/85114620db0bdc0e
>
> You can fix it by removing the `childs: [],` line from your
> `Class.create` call and then adding:
>
>    this.childs = [];
>
> ...in your `initialize` function. That will ensure that each instance
> has its own array.
>
> (Off-topic: In English, the plural of "child" is "children", not
> "childs". Although most English nouns are made plural by adding an "s"
> or "es" or even "ses", quite a number of them are done differently.)
>
> Hope this helps,
> --
> T.J. Crowder
> Independent Software Engineer
> tj / crowder software / com
> www / crowder software / com
>
> On Dec 8, 2:05 pm, Bart <[email protected]> wrote:
>> Hi,
>>
>> I'm creating a visual tree structure in which every item can have
>> multiple childs. In the following code the parent item adds one child
>> to itself. But for some reason the child adds itself as a child of its
>> own in an infinite loop:
>>
>> ----- CODE ------
>> var Item = Class.create({
>>
>>             column: null,
>>             text: null,
>>             childs: [],
>>             parent: null,
>>
>>             initialize: function(column, text, parent)
>>             {
>>                 this.column = column;
>>                 this.text = text;
>>                 this.parent = parent;
>>
>>             },
>>
>>             addChild: function(child)
>>             {
>>                 this.childs[this.childs.length] = child;
>>             }
>>
>>         });
>>
>> var startI = new Item(1, 'start', null);
>> var vervolg1 = new Item(2, 'vervolg 1', startI);
>> var vervolg2 = new Item(2, 'vervolg 2', startI);
>>
>> startI.addChild(vervolg1);
>> startI.addChild(vervolg2);
>> -----------------------------------------
>>
>> You can find an image of the result (in the console) 
>> onhttp://www.bartblok.com/problem.PNG
>> Why is this an infinite reference? The code should add vervolg1 and
>> vervolg2 as childs of startI, but not childs of themselves.
>>
>> I hope someone can help me :)

http://pastebin.com/gZ97fiH7 shows the fix T.J. is recommending.



-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to