Behavior
Core contains a lightweight extension that builds on top of
query to provide a great way for
decoupling events and DOM manipulations from an HTML placeholder via
the behavior module. It may not be
intuitively obvious at first, but the ability to define
behavior for nodes irrespective of the markup
itself can lend an immense of flexibility to a design. For example, it
allows you to concisely accomplish tasks such as assigning click
handlers to all anchor elements without knowing where or how many
anchor elements there will be. You use the same CSS selectors you
learned about in Table 5-1 to
find the nodes for attaching behavior to, so the possibilities are
almost endless.
The behavior module currently
provides two API calls; the add
method allows you to queue up a collection of behaviors, and the
apply method actually triggers
those behaviors:
dojo.behavior.add(/*Object*/ behaviorObject) dojo.behavior.apply( )
Basically, you use add to
assign a new behavior to a collection of DOM nodes, but the behavior
isn't actually reflected until you call apply. One of the reasons that it's a
two-step process is because the pattern of performing multiple
add operations before a final
apply occurs lends itself to a lot
of asynchronous communication patterns, described in Chapter 4.
Tip
Chapter 4 introduced a
data structure called Deferred
that is a staple in Dojo's IO subsystem. Deferred s provide the façade of having a thread available to operate on and lend themselves to successively applying ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access