Skip to content

Commit

Permalink
throw error if ko.applyBinding called before DOM is loaded. fixes #1215
Browse files Browse the repository at this point in the history
  • Loading branch information
mbest committed Dec 3, 2016
1 parent 1523efb commit d2a9e44
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/binding/bindingAttributeSyntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,15 @@
jQueryInstance = window['jQuery'];
}

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
// rootNode is optional
if (!rootNode) {
rootNode = window.document.body;
if (!rootNode) {
throw Error("ko.applyBindings: could not find window.document.body; has the document been loaded?");
}
} else if (rootNode.nodeType !== 1 && rootNode.nodeType !== 8) {
throw Error("ko.applyBindings: first parameter should be your view model; second parameter should be a DOM node");
}

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

0 comments on commit d2a9e44

Please sign in to comment.