Is there any feedback on ko.applyBindings?

Using in our current project, we have already come to this question several times.

How can I make sure that some piece of Javascript code is executed only after all the bindings on the page have been applied to the knockout?

In my specific use case, I use if-bindings to evaluate some configuration options and decide whether elements should be displayed inside (= in the DOM) or not. Only after these grades ifhave been evaluated, do I need to count the number of DOM nodes within a specific element. Obviously, if I think too early that the bindings ifhave not yet removed these unwanted DOM nodes, so the calculation comes up with the wrong result.

+4
source share
2 answers

, . ( ) "afterRender". , . jsfiddle ( ):

var model = {
  afterRenderCallback: function() {
    // this method will be called after content rendered
    var divContent = document.getElementById("textdiv").innerHTML;
    alert(divContent);
  },
  txt: ko.observable("this text will be substituted in the div")
};

ko.applyBindings(model);
.content {
    border: 1px solid red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

<script type="text/html" id="wrappingTemplate">
<div id="textdiv" class="content" data-bind="text: txt"></div>
</script>

<!-- ko template: { name: 'wrappingTemplate', afterRender: afterRenderCallback } -->
<!-- /ko-->
Hide result
+4

ko.applyBindings() , . , . ,

var vm = new ViewModel();
ko.applybindings(vm);
//
CountRenderedElements();

.

+6

All Articles