Creating Children
TargetJS allows children to be declared directly as named objects inside a parent. Children can also be generated dynamically, either concurrently or progressively. Additionally, TargetJS can split an existing element into multiple children.
Defining Named Children
The simplest way to create a child is to define a named object inside its parent. TargetJS automatically recognizes the object as a child TModel when it contains target definitions.
Children can also contain other children, allowing complex hierarchical structures to be defined through nested objects.
The console output will be:
businessCard true true true
In this example, businessCard is a child of the application's
root TModel, while name and email are children
of businessCard.
The html target supports HTML content, but using plain text
whenever possible is recommended, allowing TargetJS to optimize DOM
rendering. By default, children use a div element, but
this can be changed using the element target.
The done$$ target runs after the preceding operations have completed,
including the required child operations and DOM initialization.
Children can be accessed by name or index using getChild().
The hasDom() method checks whether a TModel currently has an
associated DOM element.
Creating Multiple Children
The children and addChildren targets allow
multiple children to be generated dynamically. Instead of defining
each child separately, a value function can return child definitions
that TargetJS converts into TModels.
Multiple children can be generated in a single execution using
instances, or progressively over time using
cycles, with an optional interval
specifying the delay between successive executions.
Creating Multiple Concurrently
The instances property specifies how many children
to generate during one target execution. The value()
function receives an instance index, allowing each child to have
different properties.
This example creates eight circular children in one execution.
Each child receives a different horizontal position based on its
instance index. Their backgroundColor targets animate
concurrently from orange to mediumpurple.
Once all children have completed their initial background animations, the
onComplete() callback is triggered. It uses
getChildren() to access the generated children and
setTarget() to animate their vertical positions.
Creating Multiple Progressively
The cycles property creates children over successive
executions. Unlike instances, each cycle runs separately,
allowing children to appear progressively.
Here, eight children are added one after another, with an interval of 200 milliseconds between executions.
As in the previous example, onComplete() is triggered
once all cycles have finished, the children have been created,
their DOM elements are ready, and their animations have completed.
Progressive creation is useful for staggered visual effects, dynamically loaded content, and interfaces where elements should appear over time.
Exploding Children
The explodeChildren target provides another way to
generate children by using an existing html element as the source for
many children.
Unlike instances and cycles, which
generate children from returned definitions,
explodeChildren starts with an existing TModel.
This makes it useful for visual effects that transform a single
element into multiple independently animated pieces.
For example, an image can be divided into multiple pieces that scatter, rotate, scale, or fade independently. Each generated piece becomes a child TModel that participates in the normal TargetJS execution and animation system.
The fetchImage target first loads the image. Image loading
is covered in greater detail on the Loading Images page.
Once the image is ready, explodeChildren$$ uses it
as the source for generating children.
The image is divided into smaller cells, each becoming an independently managed child TModel.
The appear$$ target reveals the cells sequentially using
cycles. Once all cells have appeared,
arrange$$ spreads the images apart.
Managing Children
TargetJS provides methods for accessing, modifying, and removing children after they have been created.
The getChild() method retrieves a child by name
or index, while getChildren() returns the
collection of children belonging to a TModel.
Children can be removed using removeChild(tmodel) to remove
an individual child, or removeChildren() to remove all children
belonging to a TModel.