Skip to content

Commit

Permalink
Gracefully handle attempts to use ko.dataFor and ko.contextFor with n…
Browse files Browse the repository at this point in the history
…on-elements
  • Loading branch information
SteveSanderson committed Aug 23, 2011
1 parent 517edfa commit 0bfc249
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/binding/bindingAttributeSyntax.js
Expand Up @@ -173,9 +173,15 @@

// Retrieving binding context from arbitrary nodes
ko.contextFor = function(node) {
var context = ko.storedBindingContextForNode(node);
if (context) return context;
if (node.parentNode) return ko.contextFor(node.parentNode);
// We can only do something meaningful for elements and comment nodes (in particular, not text nodes, as IE can't store domdata for them)
switch (node.nodeType) {
case 1:
case 8:
var context = ko.storedBindingContextForNode(node);
if (context) return context;
if (node.parentNode) return ko.contextFor(node.parentNode);
break;
}
return undefined;
};
ko.dataFor = function(node) {
Expand Down

0 comments on commit 0bfc249

Please sign in to comment.