mbest/knockout.punches

Enable two-way binding for "value" and "checked"

Closed this issue · 4 comments

In current version, "value" and "checked" bindings are converted to attr binding like the others.

<input type="text" value="{{ firstName }}">

is converted to:

<input type="text" data-bind="attr.value: firstName">

As these attributes are writable, it would be great to convert them in this way:

<input type="text" data-bind="value: firstName">

Good point. I'm thinking of making the attribute interpolation extensible anyway, and this would be a good first step in using that interface.

that'd be nice. The (hackish) way I do it with the current version is:
in the attributeInterpolationMarkerPreprocessor method, I convert this:

attrBinding = 'attr.' + attr.name + ':' + attrBinding;

into this:

if (attr.name == 'value') {
    attrBinding = attr.name + ':' + attrBinding + ", valueUpdate: 'keyup'";
}
else if (attr.name == 'checked') {
    attrBinding = attr.name + ':' + attrBinding;
}
else {
    attrBinding = 'attr.' + attr.name + ':' + attrBinding;
}

This would be really useful. Maybe as a parameter to ko.punches.attributeInterpolationMarkup.enable?

I've changed the code to use a matching binding if it exists. Thus if you have value="{{firstName}}", it will convert to data-bind="value: firstName".