Interesting question!
I'm gonna go out here and say that you need to do the DOM traversal yourself. There is no Knockout util that does exactly what you want. And even then you'll have to hook deep into KO. This is based on some experience, as well as carefully peering through the KO TypeScript definition (which is a probably a near complete overview of KO's exported functionality).
Looking at the relevant bit in the definition, you can utilize the KnockoutBindingProvider
like this:
var vm = {
submodel: {
name: ko.observable('apple'),
description: ko.observable('fruit')
},
elementsWithTextBindings: ko.observable('')
};
vm.refresh = function() {
var result = "";
var all = document.getElementsByTagName("*");
for (var i=0, max=all.length; i < max; i++) {
var ctx = ko.contextFor(all[i]);
if (ko.bindingProvider['instance'].nodeHasBindings(all[i])
&& !!ko.bindingProvider['instance'].getBindings(all[i], ctx).text) {
var bindings = ko.bindingProvider['instance'].getBindingsString(all[i], ctx);
result += "Elem with id=" + all[i].id + " has `text` binding ('" + bindings + "').\n";
}
}
vm.elementsWithTextBindings(result);
};
ko.applyBindings(vm);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div id="a" data-bind="with: submodel">
<p id="b" data-bind="text: name, style: { color: 'red' }"></p>
<p id="c" data-bind="text: description"></p>
<input id="d" data-bind="value: name" />
</div>
All elements with `text` bindings:
<button id="e" data-bind="click: refresh">refresh!</button>
<pre id="f" data-bind="text: elementsWithTextBindings"></pre>
This utilizes the fact that you can reach ko.bindingProvider
from the outside. This seems to be by design, as the source exports it with:
ko.exportSymbol('bindingProvider', ko.bindingProvider);
In my code I also utilize nodeHasBindings
, and getBindings
, and getBindingsString
. The latter has a comment:
// The following function is only used internally by this default provider.
// It's not part of the interface definition for a general binding provider.
So I'd assume the first two methods are part o the public interface and can thus be safely used for your purposes. The getBindingsString
isn't really necessary for your purpose anyways, but I've included in the example just for the example's sake.