Last active
August 29, 2015 14:02
-
-
Save gaevoy/0ede24260a50b8d156eb to your computer and use it in GitHub Desktop.
ko.getUnusedFields(el)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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