is ko.applyBindings synchronous or asynchronous?
Asked Answered
B

2

9

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

Thanks!

Ba answered 16/1, 2013 at 15:15 Comment(0)
C
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.

Closure answered 16/1, 2013 at 15:47 Comment(2)
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!Hattie
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.Closure
L
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.

Lynd answered 10/9, 2013 at 10:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.