问题
Does the generated view exists right after you call ko.applyBindings()
or does the scaffolding happen asynchronously?
Thanks!
回答1:
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.
回答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.
来源:https://stackoverflow.com/questions/14361615/is-ko-applybindings-synchronous-or-asynchronous