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

manage checked and value properties of INPUT elements #18

Closed
wants to merge 2 commits into from
Closed
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
17 changes: 16 additions & 1 deletion src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,9 @@

// add new or mutate existing not matching old
// also handles diffing of wrapped event handlers via exposed original (_fn)
if (!(name in op) || np[name] !== op[name])
if (isInputProp(targ.tagName, name))
targ[name] = np[name];
else if (!(name in op) || np[name] !== op[name])
set(targ, name, np[name], ns, init);
}
// remove any removed
Expand All @@ -952,6 +954,19 @@
}
}

function isInputProp(tag, name) {
switch (tag) {
case 'INPUT':
return name === 'value' || name === 'checked';
case 'TEXTAREA':
return name === 'value';
case 'SELECT':
return name === 'value' || name === 'selectedIndex';
case 'OPTION':
return name === 'selected';
}
}

// function setEvt(targ, name, val) {targ.addEventListener(name, val, false);}; // tofix: if old node exists (grafting), then don't re-add
// function delEvt(targ, name, val) {targ.removeEventListener(name, val, false);};

Expand Down
15 changes: 15 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,21 @@ QUnit.module("Attrs/Props");
var expcHtml = '<input type="checkbox" id="check2">';
evalOut(assert, checkEl, domvm.html(check2.vm.node), expcHtml, callCounts, { removeAttribute: 1 });
});

QUnit.test("Input Attribute", function(assert) {
var model = new Check('check3', true);
var view = domvm.view(CheckView, model).mount(testyDiv);
var el = view.node.el;

// user interaction
el.checked = false;

// redraw with model.checked still true
view.redraw();

// the visual state should be equal to the model state
assert.equal(el.checked, model.checked);
});
})();

// TODO: assert triple equal outerHTML === domvm.html(vm.node) === hardcoded html
Expand Down