9

Does the generated view exists right after you call ko.applyBindings() or does the scaffolding happen asynchronously?

Thanks!

vtortola
  • 34,709
  • 29
  • 161
  • 263

2 Answers2

12

ko.applyBindings is a synchronous call.

There may be cases where bindings have special code to do things in a setTimeout, but this is not generally the case.

With the addition of components in Knockout 3.2, components are asynchronous. With Knockout 3.3, there will be an option to render components synchronously if the viewmodel / template is loaded.

Allen Rice
  • 19,068
  • 14
  • 83
  • 115
RP Niemeyer
  • 114,592
  • 18
  • 291
  • 211
  • Ryan, could we get an update on this answer now that components are out in 3.2? I'm assuming that it's not synchronous if the module has not yet been loaded, but, is it synchronous if all the dependencies have already been loaded? Thanks man! – Allen Rice Oct 08 '14 at 05:35
  • 3
    `ko.applyBindings` itself is synchronous. components are async. In KO 3.3, there will be an option to render components sync, if viewmodel/template is loaded. – RP Niemeyer Oct 08 '14 at 13:07
  • Thanks so much for this clarification Ryan! – Allen Rice Oct 08 '14 at 19:58
2

Knockout is synchronous. Not only the ko.applyBindings function as RP Niemeyer already said. When you set a value to an observable property which is binded to a view, you can be 100% sure that after executing

myViewModel.myObservableProperty(newValue);

your view's been updated. In fact, an observable property is a function and when you set a new value to your observable property you are simply calling a function with the new value as parameter: this function in its body will trigger synchronously the 'change' event (i don't know exactly the event's name).

Hope it helps.. sorry but my English is a bit rusty.

andrea.rinaldi
  • 1,167
  • 1
  • 13
  • 33