instance method Spah.DOM.Document#addModifiers
Spah.DOM.Document#addModifiers(modifierOrArrayOfModifiers, modifer1, modifierN) → void
- modifierOrArrayOfModifiers (Object, Array): The modifier object to be registered. Expected to implement the modifier interface.
- modifier1 (Object): If modifierOrArrayOfModifiers is not an array, then all arguments passed to this method will be added as modifiers.
The interface requires your module to contain the methods:
- actionName(element, $, window) Returns the action name for this module. This is the attribute you want your modifier to respond to - for instance, the element ID modifier is interested in attributes like “data-id-foo-if”, and therefore the action name is “id”. Receives a jQuery containing the element in question as the only argument.
- up(element, flags, state, $, window) Runs the modification forwards. Used when the associated assertion flips from false to true for if assertions and when the associated assertion flips from true to false for unless assertions. The method will receive a jQuery containing the element, the state object and any flags. Flags are derived from the attribute - if we use the attribute
data-class-foo-bar-if
the actionName will be “class” and the flags will be “foo-bar”. The up and down methods are expected to interpret the flags as appropriate. - down(element, flags, state, $, window) Runs the modification backwards. Called when the associated assertion flips from true to false for if assertions and when the associated assertion flips from false to true for unless assertions. Receives the same arguments as
up
In each case the arguments are as follows:
- “element” is a jQuery containing the element in question
- “flags” are any arguments given by the attribute name (see below)
- “state” is the Spah state (a Spah.SpahQL object)
- ”$” is the main jQuery object itself
- “window” is the context DOMWindow for the document runner. Call window.document for the document itself.
Regarding flags, let’s take a look at the ClassName modifier when it processes the attributes data-class-foo-bar-if="/foo/bar"
and data-class-baz-unless="/notbaz"
The ClassName modifier returns an actionName of “class” for all elements. When Spah’s document runner encounters this attribute, the ClassName modifier is matched and passed “foo-bar” as the flags for the first attribute and “baz” for the second attribute.
It is up to you how your modifiers process flags when they are asked to run up or down.
Modifiers may also add new capabilities to the document. If your modifier includes the “added” method, Spah will call this function when the modifier is added to the document. The function receives the document instance as an argument. You may use this to extend the document’s capabilities - for example Spah’s built-in Populate modifier adds the addTemplate and removeTemplate methods to the document instance, allowing you to register mustache templates with the document.