-
Notifications
You must be signed in to change notification settings - Fork 1
Behavior Composition
A single element can have more than one Behavior applied to it. There’s two major ways this can happen. First, an Array of Behaviors is a valid behavior-resolvable object, and each of the Behaviors will be applied to selected elements. Second, more than one selector might match the element (simply by being identical, or by matching different features), in which case, all the Behaviors selected will be applied to the element.
In order for the outcome of multiple Behaviors being applied to be predictable (or at least consistent), we need to know what order the Behaviors will be applied in. Here are the rules Ninjascript uses to decide that:
- Behaviors are applied in increasing priority order
- That is: -1, 0, 1
- Undefined priority is equivalent to 0
- Where Behaviors have the same priority, they’re applied in the order of definition.
When you’re chaining behaviors, either on purpose the way that Ninja.becomesAjaxLink()
does, or by coincidence, the way that you compose CSS styles, the order of application has several impacts. First, each Behavior has a transform function, and they need to be applied in a consistent fashion. Second, the order in which event handlers have a shot at an event is important. Priorities make these compositions consistent, so that they’re predictable.