Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Implement two-way binding to HTMLTextArea.value
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelw committed Jun 1, 2013
1 parent 5846863 commit 55104c3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 56 deletions.
81 changes: 38 additions & 43 deletions src/template_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,48 +441,72 @@
});

function bindInput(name, model, path) {
switch(name) {
case 'value':
switch(this.tagName + '.' + name.toLowerCase()) {
case 'INPUT.value':
case 'TEXTAREA.value':
this.unbind('value');
this.removeAttribute('value');
valueBindingTable.set(this, new ValueBinding(this, model, path));
break;
case 'checked':
case 'INPUT.checked':
this.unbind('checked');
this.removeAttribute('checked');
checkedBindingTable.set(this, new CheckedBinding(this, model, path));
break;
case 'SELECT.selectedindex':
this.unbind('selectedindex');
this.removeAttribute('selectedindex');
valueBindingTable.set(this, new SelectedIndexBinding(this, model, path));
break;
default:
return Element.prototype.bind.call(this, name, model, path);
break;
}
}

function unbindInput(name) {
switch(name) {
case 'value':
switch(this.tagName + '.' + name.toLowerCase()) {
case 'INPUT.value':
case 'TEXTAREA.value':
var valueBinding = valueBindingTable.get(this);
if (valueBinding) {
valueBinding.unbind();
valueBindingTable.delete(this);
}
break;
case 'checked':
case 'INPUT.checked':
var checkedBinding = checkedBindingTable.get(this);
if (checkedBinding) {
checkedBinding.unbind();
checkedBindingTable.delete(this)
}
break;
case 'SELECT.selectedindex':
var valueBinding = valueBindingTable.get(this);
if (valueBinding) {
valueBinding.unbind();
valueBindingTable.delete(this);
}
break;
default:
return Element.prototype.unbind.call(this, name);
break;
}
}

function unbindAllInput(name) {
this.unbind('value');
this.unbind('checked');
switch (this.tagName) {
case 'INPUT':
this.unbind('checked');
// fallthrough
case 'TEXTAREA':
this.unbind('value');
break;
case 'SELECT':
this.unbind('selectedindex');
break;
}

Element.prototype.unbindAll.call(this);
}

Expand Down Expand Up @@ -523,42 +547,13 @@
}
});

function bindSelect(name, model, path) {
switch(name.toLowerCase()) {
case 'selectedindex':
this.unbind('selectedindex');
this.removeAttribute('selectedindex');
valueBindingTable.set(this, new SelectedIndexBinding(this, model, path));
break;
default:
return Element.prototype.bind.call(this, name, model, path);
break;
}
}

function unbindSelect(name) {
switch(name.toLowerCase()) {
case 'selectedindex':
var valueBinding = valueBindingTable.get(this);
if (valueBinding) {
valueBinding.unbind();
valueBindingTable.delete(this);
}
break;
default:
return Element.prototype.unbind.call(this, name);
break;
}
}

function unbindAllSelect(name) {
this.unbind('selectedindex');
Element.prototype.unbindAll.call(this);
}
HTMLSelectElement.prototype.bind = bindInput;
HTMLSelectElement.prototype.unbind = unbindInput;
HTMLSelectElement.prototype.unbindAll = unbindAllInput;

HTMLSelectElement.prototype.bind = bindSelect;
HTMLSelectElement.prototype.unbind = unbindSelect;
HTMLSelectElement.prototype.unbindAll = unbindAllSelect;
HTMLTextAreaElement.prototype.bind = bindInput;
HTMLTextAreaElement.prototype.unbind = unbindInput;
HTMLTextAreaElement.prototype.unbindAll = unbindAllInput;

var BIND = 'bind';
var REPEAT = 'repeat';
Expand Down
34 changes: 21 additions & 13 deletions tests/node_bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,31 +176,39 @@ suite('Form Element Bindings', function() {
setup(doSetup);
teardown(doTeardown);

test('Input.value', function() {
var input = testDiv.appendChild(document.createElement('input'));
function inputTextAreaValueTest(type) {
var el = testDiv.appendChild(document.createElement(type));
var model = {x: 42};
input.bind('value', model, 'x');
assert.strictEqual('42', input.value);
el.bind('value', model, 'x');
assert.strictEqual('42', el.value);

model.x = 'Hi';
assert.strictEqual('42', input.value);
assert.strictEqual('42', el.value);
Platform.performMicrotaskCheckpoint();
assert.strictEqual('Hi', input.value);
assert.strictEqual('Hi', el.value);

input.value = 'changed';
dispatchEvent('input', input);
el.value = 'changed';
dispatchEvent('input', el);
assert.strictEqual('changed', model.x);

input.unbind('value');
el.unbind('value');

input.value = 'changed again';
dispatchEvent('input', input);
el.value = 'changed again';
dispatchEvent('input', el);
assert.strictEqual('changed', model.x);

input.bind('value', model, 'x');
el.bind('value', model, 'x');
model.x = null;
Platform.performMicrotaskCheckpoint();
assert.strictEqual('', input.value);
assert.strictEqual('', el.value);
}

test('Input.value', function() {
inputTextAreaValueTest('input');
});

test('TextArea.value', function() {
inputTextAreaValueTest('textarea');
});

test('Input.value - user value rejected', function() {
Expand Down

0 comments on commit 55104c3

Please sign in to comment.