Book Image

Mastering KnockoutJS

By : Timothy Moran
Book Image

Mastering KnockoutJS

By: Timothy Moran

Overview of this book

Table of Contents (16 chapters)
Mastering KnockoutJS
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Node preprocessors


Binding handler preprocessors are attached to individual binding handlers and work by modifying the binding string. They only apply to nodes of their respective handler.

Node preprocessors, on the other hand, are called on every DOM node. They run when the UI is first bound and when it is modified by bindings such as foreach or template.

The purpose of a node preprocessor is to modify the DOM before data-binding occurs, as opposed to a binding preprocessor that only modifies the data-bind attribute. A node preprocessor is defined by adding a preprocessNode function to the binding provider:

ko.bindingProvider.instance.preprocessNode = function(node) {
  /* DOM code */
}

A preprocessor is called once for each node. If no changes need to be made, it should return nothing. Otherwise, it can use the standard DOM API to insert new nodes or remove the current node:

  • New nodes should be inserted before the current node by using:

    node.parentNode.insertBefore(newNode, node);
  • Replacement...