37

I have been looking but cannot find the documentation for applyBindings(). What can the second parameter legally contain? Can it be an array of elements? Must it be a single element? Can the bindings be applied to the child elements of two separate nodes by calling applyBindings twice?

       ko.applyBindings(myViewModel, div1);
       ko.applyBindings(myViewModel, div2);

3 Answers 3

49

KnockoutJS is open source. From the relevant file:

ko.applyBindings = function (viewModelOrBindingContext, rootNode) {
    // Some code omitted for brevity...

    if (rootNode && (rootNode.nodeType !== 1) && (rootNode.nodeType !== 8))
        throw new Error("ko.applyBindings: first parameter should be your view model; second parameter should be a DOM node");
    rootNode = rootNode || window.document.body; // Make "rootNode" parameter optional

    applyBindingsToNodeAndDescendantsInternal(getBindingContext(viewModelOrBindingContext), rootNode, true);
};

So yes, it seems it must be a single DOM node. To be more specific, the nodeType is must be either 1 (ELEMENT_NODE) or 8 (COMMENT_NODE), otherwise an Error is thrown.

The relevant documentation ("Activating Knockout") is less explicit that it must be a DOM node, but (see emphasis, added by me) does kind of say the same thing:

Optionally, you can pass a second parameter to define which part of the document you want to search for data-bind attributes. For example, ko.applyBindings(myViewModel, document.getElementById('someElementId')). This restricts the activation to the element with ID someElementId and its descendants, which is useful if you want to have multiple view models and associate each with a different region of the page.

As long as nodes don't share part of the tree (e.g. they're siblings) you can call applyBindings safely on each of the nodes (in fact, that's one reason to use the second argument).

See this related question for a typical use case.

1
  • 3
    Adding the specific KO source code in the answer was nice. Dec 17, 2014 at 15:22
11

Can be found at the following link... http://knockoutjs.com/documentation/observables.html

In case you’re wondering what the parameters to ko.applyBindings do,

The first parameter says what view model object you want to use with the declarative bindings it activates

Optionally, you can pass a second parameter to define which part of the document you want to search for data-bind attributes. For example, ko.applyBindings(myViewModel, document.getElementById('someElementId')). This restricts the activation to the element with ID someElementId and its descendants, which is useful if you want to have multiple view models and associate each with a different region of the page.

2

In case anyone is looking to use classes for implementing this

for i of $('.myView')
  ko.applyBindings(new MyView(),$('.myView')[i])
1
  • this code create n instances of MyView, which are not related to each other. In many cases this be a bad solution Nov 10, 2016 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.