Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
knockout and applyBindings - how to control the scope?

I am new to knockout, so going to fire a lot of questions. But I'm not new to data binding. So I am knocking my head on the difference between my expectations and reality. Here is a very basic question about applyBindings.

It looks like applyBindings has the parameter "view model", which is to me the domain object graph (as javascript objects) plus perhaps additional helper things added for the purpose of the view creation. But what I am completely missing at first is the scope of the bindings! I expected this to be applied to the current parent DOM element. But no, it is applied globally, in the entire page!

So is the expectation that in one HTML document there can only ever be one view model? This is very surprising to me! How am I supposed to create a single page web app where I have one panel showing the address book, another panel showing my appointments, another panel showing one loan application to review, and yet another showing the underwriting of another loan? They are all completely different things, am I really supposed to link them all into a single view model???

In my expectation, you bind a javascript object to a DOM element, and everything in there renders it. With every new nested DOM element, the focus object may change. It may be flowing out of some foreach binding from the parent's object. But then two sibling (or cousin) DOM elements might be sitting side by side and having completely different view model, and also a different life cycle. Like while I am in my underwriting workflow, I quickly need to bring up an address book or my calendar. All of it in a single page app. There should be no global interference between different view models used by different unrelated DOM elements.

And yet here we are with knockout I see it has only one ko.applyBindings(viewModelObject) for a the entire page.

What am I missing? What is preventing us from modifying ko.applyBindings to take two arguments, the view model-object and the DOM element in which to show it? I could try doing that, but I am afraid if knockout has been designed in this global mindset, there might be lots of issues running the knockout machinery more than once on the same page?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'm sorry if I am frustrating people by shooting an answer already. But since I have a very urgent project I need to try to resolve my issues ASAP, and I am reading the knockout source code, which is quite nicely organized and uses good names, so it's quite intelligible; therefore I have found the answer myself.

The answer is that, yes, you can applyBindings to a parent element that you choose. The document.body is only the default if you don't say anything.

Therefore, from now on, I shall (tell my team to) always call applyBindings with the second argument specified for the rootNode. Like this:

<div>
  ... all my UI elements for this thing ...
  ... then last element in this div:
  <script type="text/javascript">ko.applyBindings(viewModelObject, document.currentScript.parentElement);</script>
</div>

so, that way I can have multiple view models each in their own DOM element.

And additionally also, I was wondering, does knockout not somehow assign the model object to the DOM element? I could do that in my script tag too:

<div>
  ... all my UI elements for this thing ...
  ... then last element in this div:
  <script type="text/javascript">
     const viewElement = document.currentScript.parentElement;
     viewElement.viewModelObject = viewModelObject;
     ko.applyBindings(viewModelObject, viewElement);
  </script>
</div>

and this allows me then -- if only for debugging -- to find the current view model object on the DOM element that is the root of a view. (It would be nice if that would happen with all other descendant bindings too, but that is perhaps the subject of another question.)

In fact, I decided to put into our general configuration a hard replacement of the ko.applyBindings function:

ko._applyBindings = ko.applyBindings;
ko.applyBindings(viewModelObject, rootNode, extendContextCallback) {
    rootNode = rootNode || document.currentScript.parentElement;
    rootNode.viewModelObject = viewModelObject;
    ko._applyBindings(viewModelObject, rootNode, extendContextCallback);
}

now I don't even have to convince my team to do it this way, they will automatically, even without being aware of it.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda