Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for radio buttons #62

Merged
merged 3 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ The `init` binding can be used with the following bindings:
- `value`: the value is set to the bound element's value.
- `html`: the value is set to the bound element's inner HTML value.
- `attr`: the bound attribute properties are set to their respective attribute values.
- `checked`: the value is set to the bound element's checked value.
- `checked`: the value is set to the bound element's checked value of both checkbox and radio inputs.
- `visible`: the value is set to `true` or `false` depending on the bound element's visibility.
- `enable`: the value is set to `true` if the bound element does not have a `disabled` attribute; otherwise, it is set to `false`.
- `disable`: the value is set to `true` if the bound element has a `disabled` attribute; otherwise, it is set to `true`.
Expand Down Expand Up @@ -311,7 +311,8 @@ There is a JSBin demo for each of the binding handlers:
- [`init` with value binding](http://jsbin.com/xuluye/)
- [`init` with html binding](http://jsbin.com/wilahi/)
- [`init` with attr binding](http://jsbin.com/zehivi/)
- [`init` with checked binding](http://jsbin.com/zemode/)
- [`init` with checked binding for checkbox](http://jsbin.com/zemode/)
- [`init` with checked binding for radio](http://jsbin.com/pumodetaqu/)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have one suggestion: the UI currently displays "Plays" before each radiobutton. Could you add what is being played to the text?

- [`init` with visible binding](http://jsbin.com/vufufa/)
- [`init` with enable binding](http://jsbin.com/moxoji/)
- [`init` with disable binding](http://jsbin.com/bonapi/)
Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Test against this version of Node.js
environment:
nodejs_version: "4.1"
nodejs_version: "16"

# Install scripts. (runs after repo cloning)
install:
Expand All @@ -18,4 +18,4 @@ test_script:
- npm test

# Don't actually build.
build: off
build: off
12 changes: 9 additions & 3 deletions knockout-pre-rendered.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,8 @@
extendBindingInit("text");
extendBindingInit("textInput");
extendBindingInit("checked", function(element) {
if(element.type == 'radio')
return element.value;
return element.checked;
});
extendBindingInit("visible", function(element) {
Expand Down Expand Up @@ -690,7 +692,8 @@
convert,
bindingName,
allBindings,
propertyWritersBindingName
propertyWritersBindingName,
element.type =='radio' && !element.checked
);
}
} else {
Expand Down Expand Up @@ -722,7 +725,8 @@
convert,
key,
allBindings,
propertyWritersBindingName
propertyWritersBindingName,
shouldIgnore
) {
if (convert) {
var conversion = convert.hasOwnProperty(key) ? convert[key] : convert;
Expand All @@ -732,7 +736,9 @@
}

if (ko.isObservable(fieldAccessor)) {
fieldAccessor(value, ko.unwrap(fieldAccessor));
if(!shouldIgnore)
fieldAccessor(value, ko.unwrap(fieldAccessor));

} else {
var propertyWriter = getPropertyWriter(
allBindings,
Expand Down
18 changes: 18 additions & 0 deletions test/init-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,24 @@ describe("init binding", function() {
expect($(target).attr("checked")).to.equal(undefined);
expect(model.visited()).to.be.false;
});

it("works with unchecked radio buttons", function() {
var target = $(
"<input data-bind='init, checked: city' type='radio' value='Chennai' />"
);
var model = new Models.ViewModel();
ko.applyBindings(model, target[0]);
expect(model.city()).to.equal('');
});

it("works with checked radio buttons", function() {
var target = $(
"<input data-bind='init, checked: city' type='radio' value='Chennai' checked/>"
);
var model = new Models.ViewModel();
ko.applyBindings(model, target[0]);
expect(model.city()).to.equal('Chennai');
});
});

describe("combined with visible binding", function() {
Expand Down