Knockout: valueAccessor vs viewModel on custom bin

2019-04-08 02:06发布

问题:

What's the difference between getting access to a view model's values through the binding properties (via valueAccessor) and getting them from the viewModel argument passed to the handler's init and update? I have always assumed the answer is that those model properties linked to valueAccessor automatically set up bindings (which would fire an update on any change to observables) but I'm not sure about that.

回答1:

The valueAccessor argument gives you access to specifically what was passed into the binding, while the viewModel argument gives you access to the entire data object at the scope.

So, if someone says text: firstName

valueAccessor() would give you the firstName observable/property and viewModel would give you access to the object that contains firstName.



标签: knockout.js