bindingContext is a feature, but only for transferring data from init to update when the binding is first started. The next time update fires, it will be gone.
In fact, there are two options for maintaining this type of state:
1- On the item as you stated. You can use jQuery $.data , or KO includes an API for this, as well as ko.utils.domData.get(element, key) and ko.utils.domData.set(element, key, value) .
2- Put this type of information in your view model, if necessary. The flag for isEditing not necessarily inappropriate in the view model. I personally would like to put this type of "metadata" as sub-observables from the observable type:
var name = ko.observable("Bob"); name.isEditing = ko.observable(false);
You could bind to name and name.isEditing .
This has some advantages:
- keeps the review model pretty clean since you are not introducing new top-level properties.
- saves sub-observable attached to parent observable (no need for
nameIsEditing , etc.) - when it turns into JSON with something like
ko.toJSON , the sub-observable isEditing will simply be deleted when the parent is unpacked. This way you will not send unnecessary values โโback to the server. - in this case, it may also have the advantage of being available for other calculations in your view model or for binding to multiple elements of your user interface.
RP Niemeyer Apr 17 '12 at 1:17 2012-04-17 01:17
source share