Accessing parent container.

How would one access the parent container from within a child container?

I have the following container hierarchy: parentContainer{ ChildContainer1 ChildContainer2 }

I have an onTap event for ChildContainer2, using which I want to change a property of ChildContainer1.

How would I go about doing this? Thank you!

Answers

  • Something like this:

    onTap: function(content){
        var parent = content.container; //this is how you get the parent
        var firstChild = parent.first; //this is how you get the first child
        var lastChild = parent.last; //this is how you get the last child
        var arbChild = parent.NamedChild; //this is how you get a child with a particular name, set with name="NamedChild"
    }
    

    — Andy

  • `

    var DropDownMenu = Container.template(function($){ return { 
        bottom : 5, right: 5, top: 5,  left : 5, skin: graySkin, active: true, 
        contents: [ 
                   new Line({left: 0, right: 0, top: 0, bottom: 0,  
                       contents: [ 
                            new Label({top : 0, bottom : 0, left : 0, right : 0,  
                                style:textStyle, active: true, string:'Fast', 
                                                        toString : 'Id1'
                            }), 
                            new Picture({left:0, right:0, top:0, bottom:0, url : "down.png", active : true, 
                                behavior: Object.create(Behavior.prototype, { 
                                    onTouchEnded: { value: function(content, id, x,  y, ticks) {                                     
                                        dropDownVisible = true;      
                                        var oldScreen = currentScreen; 
                                        currentScreen = new screen4;                                                                                                     
                                        trace("Dropdown was pressed" + dropDownVisible + " \n");                                     
                                        if($.object = "Number") 
                                        { 
                                            application.add(new screenNumber);                                                                               
                                            AAAdropDownMenuPressed = content.container; 
                                            trace("Right place \t"); 
                                        }else if ($.object = "Units") 
                                        { 
                                            application.add(new screenUnits); 
                                        } 
                                    }} 
                                }) 
                            }),             
                       ] 
                   }) 
              ] 
    
    }})
    

    `

    I want to change the string "fast" in the first container, but cannot get access to it through AAAdropDownMenuPressed.first.first.string

    Am I accessing the parent container properly?

    Also, would setting the toString for the first container allow us to display "ID1" when using trace(this)?

  • `

    var DropDownMenu = Container.template(function($){ return { 
        bottom : 5, right: 5, top: 5,  left : 5, skin: graySkin, active: true, 
        contents: [ 
                   new Line({left: 0, right: 0, top: 0, bottom: 0,  
                       contents: [ 
                            new Label({top : 0, bottom : 0, left : 0, right : 0,  
                                style:textStyle, active: true, string:'Fast', 
                                                        toString : 'Id1'
                            }), 
                            new Picture({left:0, right:0, top:0, bottom:0, url : "down.png", active : true, 
                                behavior: Object.create(Behavior.prototype, { 
                                    onTouchEnded: { value: function(content, id, x,  y, ticks) {                                     
                                        dropDownVisible = true;      
                                        var oldScreen = currentScreen; 
                                        currentScreen = new screen4;                                                                                                     
                                        trace("Dropdown was pressed" + dropDownVisible + " \n");                                     
                                        if($.object = "Number") 
                                        { 
                                            application.add(new screenNumber);                                                                               
                                            AAAdropDownMenuPressed = content.container; 
                                            trace("Right place \t"); 
                                        }else if ($.object = "Units") 
                                        { 
                                            application.add(new screenUnits); 
                                        } 
                                    }} 
                                }) 
                            }),             
                       ] 
                   }) 
              ] 
    
    }})
    

    `

    I want to change the string "fast" in the first container, but cannot get access to it through AAAdropDownMenuPressed.first.first.string

    Am I accessing the parent container properly?

    Also, would setting the toString for the first container allow us to display "ID1" when using trace(this)?

  • Answer ✓

    You've got too many firsts in there, I think. AAAdropDownMenuPressed.first.string is what you want, I believe.

    No idea on the toString thing. Quite possibly?

  • Thanks Andy! My bad, I overlooked the Line Container.

    And I tried the toString thing, but in vain. It just traces [Object Object].

    I was able to get access the string, but changing it makes the formatting of the Picture and the Label goes haywire. (They seem to jump of screen, funnily enough).

    Is there a better way to do this?

  • Answer ✓

    Try giving the Label a fixed height instead of top: 0, bottom: 0 something like height: 40 (or whatever your font height is) might be better behaved.

  • That did the trick! Thanks Andy!

Sign In or Register to comment.