Permalink
Browse files

Gracefully handle attempts to use ko.dataFor and ko.contextFor with n…

…on-elements
  • Loading branch information...
SteveSanderson committed Aug 23, 2011
1 parent 517edfa commit 0bfc2492020dbf3ac6fbc5a9915e3309500a08f1
Showing with 9 additions and 3 deletions.
  1. +9 −3 src/binding/bindingAttributeSyntax.js
@@ -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) {

0 comments on commit 0bfc249

Please sign in to comment.