Parent-Child Relationships with _Container and _Contained

After you've been rolling with _Widget and _Templated for a while, it won't be long before you find that it's convenient to have a widget that contains some children widgets. The "has-a relationship" pattern is quite common in programming and it is no different with Dojo. The _Container and _Contained mixins are designed to facilitate the referencing back and forth between parents and children that often needs to happen. Table 12-1 summarizes the API.

Table 12-1. _Container and _Contained mixins

Name

Comment

removeChild(/*Object*/ dijit)

Removes the child widget from the parent. (Silently fails if the widget is not a child or if the container does not have any children.)

addChild(/*Object*/ dijit,

/*Integer?*/ insertIndex)

Adds a child widget to the parent, optionally using the insertIndex to place it.

getParent( )

Allows a child to reference its parent. Returns a dijit instance.

getChildren( )

Allows a parent to conveniently enumerate each of its children dijits. Returns an Array of dijit instances.

getPreviousSibling( )

Allows a child widget to reference its previous sibling, i.e., the one "to the left." Returns a dijit instance.

getNextSibling( )

Allows a child widget to reference its next sibling, i.e., the one "to the right." Returns a dijit instance.

You'll see these mixins used extensively when you learn about the layout dijits. Next, we'll look at an example.

Get Dojo: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.