Currently I am storing the state using jQuery data for the dom element.
ko.bindingHandlers.customValue = { init: function init(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { var state = { isEditing: false }; $(element).focus(function focus() { state.isEditing = true; }).blur(function blur() { state.isEditing = false; }).data("customBinding", state); }, update: function update(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { // ignore if updating if (!$(element).data("customBinding").isEditing) { // handle update if they are not updating } } };
Is there a better place to store state per binding that does not require the dom? Can the bindingContext be used to store state for the each instance of the binding?
KO is able to create a two-way binding if you use value to link a form element to an Observable property, so that the changes between them are exchanged among them. If you refer a simple property on ViewModel, KO will set the form element's initial state to property value.
unwrap method is used to read it), the current viewModel and bindingContext. Whenever the passed value changes the binding will print updated information to console. This binding cannot be used with virtual elements (in html comments), only on real elements, since ko.
Knockout is a JavaScript library that helps you to create rich, responsive display and editor user interfaces with a clean underlying data model.
The bindingContext
is a possibility, but only for passing data from init
to update
the first time that the binding is triggered. The next time that update
fires it would no longer be there.
There are really two choices for where to store this type of state:
1- On the element, as you stated. You can use jQuery's $.data
or KO includes APIs for doing this as well 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 appropriate. A flag to indicate isEditing
is not necessarily out of place in a view model. I personally like to put this type of "meta-data" as sub-observables off of an observable like:
var name = ko.observable("Bob"); name.isEditing = ko.observable(false);
You would be able to bind against name
and name.isEditing
.
This has some advantages:
nameIsEditing
, etc.)ko.toJSON
the isEditing
sub-observable will simply be dropped when its parent is unwrapped. So, you won't be sending unnecessary values back to the server.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With