dojo/behavior

Summary

Deprecated. dojo/behavior's functionality can be achieved using event delegation using dojo/on and on.selector().

A very simple, lightweight mechanism for applying code to existing documents, based around dojo/query (CSS3 selectors) for node selection, and a simple two-command API: add() and apply();

Behaviors apply to a given page, and are registered following the syntax options described by add() to match nodes to actions, or "behaviors".

Added behaviors are applied to the current DOM when .apply() is called, matching only new nodes found since .apply() was last called.

See the dojo/behavior reference documentation for more information.

Properties

Methods

add(behaviorObj)

Defined by dojo/behavior

Add the specified behavior to the list of behaviors, ignoring existing matches.

Add the specified behavior to the list of behaviors which will be applied the next time apply() is called. Calls to add() for an already existing behavior do not replace the previous rules, but are instead additive. New nodes which match the rule will have all add()-ed behaviors applied to them when matched.

The "found" method is a generalized handler that's called as soon as the node matches the selector. Rules for values that follow also apply to the "found" key.

The "on*" handlers are attached with dojo.connect(), using the matching node

If the value corresponding to the ID key is a function and not a list, it's treated as though it was the value of "found".

dojo/behavior.add() can be called any number of times before the DOM is ready. dojo/behavior.apply() is called automatically by dojo.addOnLoad, though can be called to re-apply previously added behaviors anytime the DOM changes.

There are a variety of formats permitted in the behaviorObject

Parameter Type Description
behaviorObj Object

The behavior object that will be added to behaviors list. The behaviors in the list will be applied the next time apply() is called.

Examples

Example 1

Simple list of properties. "found" is special. "Found" is assumed if no property object for a given selector, and property is a function.

behavior.add({
    "#id": {
        "found": function(element){
            // node match found
        },
        "onclick": function(evt){
            // register onclick handler for found node
        }
    },
    "#otherid": function(element){
        // assumes "found" with this syntax
    }
});

Example 2

If property is a string, a dojo.publish will be issued on the channel:

behavior.add({
    // topic.publish() whenever class="noclick" found on anchors
    "a.noclick": "/got/newAnchor",
    "div.wrapper": {
        "onclick": "/node/wasClicked"
    }
});
topic.subscribe("/got/newAnchor", function(node){
    // handle node finding when dojo/behavior.apply() is called,
    // provided a newly matched node is found.
});

Example 3

Scoping can be accomplished by passing an object as a property to a connection handle (on*):

behavior.add({
        "#id": {
            // like calling dojo.hitch(foo,"bar"). execute foo.bar() in scope of foo
            "onmouseenter": { targetObj: foo, targetFunc: "bar" },
            "onmouseleave": { targetObj: foo, targetFunc: "baz" }
        }
});

Example 4

Behaviors match on CSS3 Selectors, powered by dojo/query. Example selectors:

behavior.add({
    // match all direct descendants
    "#id4 > *": function(element){
        // ...
    },

    // match the first child node that's an element
    "#id4 > :first-child": { ... },

    // match the last child node that's an element
    "#id4 > :last-child":  { ... },

    // all elements of type tagname
    "tagname": {
        // ...
    },

    "tagname1 tagname2 tagname3": {
        // ...
    },

    ".classname": {
        // ...
    },

    "tagname.classname": {
        // ...
    }
});

apply()

Defined by dojo/behavior

Applies all currently registered behaviors to the document.

Applies all currently registered behaviors to the document, taking care to ensure that only incremental updates are made since the last time add() or apply() were called.

If new matching nodes have been added, all rules in a behavior will be applied to that node. For previously matched nodes, only behaviors which have been added since the last call to apply() will be added to the nodes.

apply() is called once automatically by dojo.addOnLoad, so registering behaviors with dojo/behavior.add() before the DOM is ready is acceptable, provided the dojo.behavior module is ready.

Calling appy() manually after manipulating the DOM is required to rescan the DOM and apply newly .add()ed behaviors, or to match nodes that match existing behaviors when those nodes are added to the DOM.

© 2005–2017 JS Foundation
Licensed under the AFL 2.1 and BSD 3-Clause licenses.
http://dojotoolkit.org/api/1.10/dojo/behavior.html