javascript - KnockoutJS: What Does Calling applyBindings Without Arguments do? -


i'm working through -- interesting -- code didn't write, , came across requirejs module seems initialize knockoutjs instance application.

define([     'ko',     './template/engine',     'knockoutjs/knockout-repeat',     'knockoutjs/knockout-fast-foreach',     'knockoutjs/knockout-es5',     './bind/scope',     './bind/staticchecked',     './bind/datepicker',     './bind/outer_click',     './bind/keyboard',     './bind/optgroup',     './bind/fadevisible',     './bind/mage-init',     './bind/after-render',     './bind/i18n',     './bind/collapsible',     './bind/autoselect',     './extender/observable_array',     './extender/bound-nodes' ], function (ko, templateengine) {     'use strict';      ko.settemplateengine(templateengine);     ko.applybindings(); }); 

this code calls ko.applybindings() without view model. i'm new knockout, all tutorials i've seen indicate need pass applybindings viewmodel object.

what calling applybindings without parameters do, exactly?

my end goal here figure out how application (magento 2, if anyone's curious) using knockout render things, i'm looking reasons knockout developer might this.

most of knockout's functionality won't make sense without viewmodel, doesn't make required. can see in the source nothing breaks when don't pass viewmodel parameter: sets binding context undefined:

function getbindingcontext(viewmodelorbindingcontext) {     return viewmodelorbindingcontext && (viewmodelorbindingcontext instanceof ko.bindingcontext)         ? viewmodelorbindingcontext         : new ko.bindingcontext(viewmodelorbindingcontext); } 

a few examples can think of why 1 applybindings without viewmodel:

  • injecting static templates
  • attaching event listener methods in global context

i must admit though these feel bit contrived... don't know why code you've shown it...

window.logclick = function(data, event) {    console.log("click");  };    ko.applybindings();
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>  <div>    <div data-bind="template: 'header-template'"></div>      <button data-bind="click: logclick">click listener in window</button>  </div>        <script id="header-template" type="text/html">      <h1>a header inserted template</h2>      </script>


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -