Skip to content

Commit

Permalink
fix checkbox name binding exception if model value is not an array in…
Browse files Browse the repository at this point in the history
… certain circumstances

the test doesn't fail without the fix, but the fix definitely fixes a
bug that happens with the exact same template construct elsewhere
  • Loading branch information
evs-chris committed May 21, 2021
1 parent 25702e5 commit d6c27cf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/view/items/element/binding/CheckboxNameBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export default class CheckboxNameBinding extends Binding {

handleChange() {
this.isChecked = this.element.node.checked;
this.group.value = this.model.get().slice();
const mval = this.model.get();
this.group.value = mval === undefined ? [] : mval.slice();
const value = this.element.getAttribute('value');
if (this.isChecked && !this.arrayContains(this.group.value, value)) {
this.group.value.push(value);
Expand Down
14 changes: 14 additions & 0 deletions tests/browser/twoway.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,20 @@ export default function() {
t.ok(!ractive.find('#green').checked);
});

test('Checkbox name bindings with an undefined nested name will build an array', t => {
const r = new Ractive({
el: fixture,
template: `{{#each paths as path}}{{#each colors as color}}<input id="{{.}}" type=checkbox name="{{~/foo[path]}}" value="{{.}}" />{{/each}}{{/each}}`,
data: {
paths: ['colors'],
colors: ['red', 'green', 'blue']
}
});

fire(r.find('#green'), 'click');
t.deepEqual(r.get('foo.colors'), ['green']);
});

test('Checkbox Name bindings use property attributes to determined if checked or not', t => {
let ractive = new Ractive({
el: fixture,
Expand Down

0 comments on commit d6c27cf

Please sign in to comment.