Skip to content

Instantly share code, notes, and snippets.

@gaevoy
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaevoy/0ede24260a50b8d156eb to your computer and use it in GitHub Desktop.
Save gaevoy/0ede24260a50b8d156eb to your computer and use it in GitHub Desktop.
ko.getUnusedFields(el)
ko.getUnusedFields = function (el) {
function isElement(obj) {
try {
//Using W3 DOM2 (works for FF, Opera and Chrom)
return obj instanceof HTMLElement;
}
catch (e) {
//Browsers not supporting W3 DOM2 don't have HTMLElement and
//an exception is thrown and we end up here. Testing some
//properties that all elements have. (works on IE7)
return (typeof obj === "object") && (obj.nodeType === 1) && (typeof obj.style === "object") && (typeof obj.ownerDocument === "object");
}
}
var vm = isElement(el) ? ko.dataFor(el) : el;
var unusedFields = [];
for (var fieldName in vm) {
var field = vm[fieldName];
if (field.getSubscriptionsCount && field.getSubscriptionsCount() == 0) {
unusedFields.push(fieldName);
}
}
return unusedFields;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment