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

Commit

Permalink
dont monkey-patch observables for input bindings and make Node.bind a…
Browse files Browse the repository at this point in the history
…rg polymorphism clearer

R=arv
BUG=

Review URL: https://codereview.appspot.com/73250043
  • Loading branch information
rafaelw committed Mar 9, 2014
1 parent 45916fe commit c3617b9
Showing 1 changed file with 41 additions and 28 deletions.
69 changes: 41 additions & 28 deletions src/NodeBind.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@
if (oneTime)
return updateText(this, value);

var observable = value;

unbind(this, 'textContent');
updateText(this, value.open(textBinding(this)));
return this.bindings.textContent = value;

updateText(this, observable.open(textBinding(this)));

return this.bindings.textContent = observable;
}

function updateAttribute(el, name, conditional, value) {
Expand Down Expand Up @@ -117,10 +121,12 @@
return updateAttribute(this, name, conditional, value);

unbind(this, name);

var observable = value;
updateAttribute(this, name, conditional,
value.open(attributeBinding(this, name, conditional)));
observable.open(attributeBinding(this, name, conditional)));

return this.bindings[name] = value;
return this.bindings[name] = observable;
};

var checkboxEventType;
Expand Down Expand Up @@ -189,15 +195,13 @@
}
input.addEventListener(eventType, eventHandler);

var capturedClose = observable.close;
observable.close = function() {
if (!capturedClose)
return;
input.removeEventListener(eventType, eventHandler);
return {
close: function() {
input.removeEventListener(eventType, eventHandler);
observable.close();
},

observable.close = capturedClose;
observable.close();
capturedClose = undefined;
observable_: observable
}
}

Expand Down Expand Up @@ -244,7 +248,7 @@
var checkedBinding = radio.bindings.checked;
if (checkedBinding) {
// Set the value directly to avoid an infinite call stack.
checkedBinding.setValue(false);
checkedBinding.observable_.setValue(false);
}
});
}
Expand All @@ -254,7 +258,6 @@
if (name !== 'value' && name !== 'checked')
return HTMLElement.prototype.bind.call(this, name, value, oneTime);


this.removeAttribute(name);
var sanitizeFn = name == 'checked' ? booleanSanitize : sanitizeValue;
var postEventFn = name == 'checked' ? checkedPostEvent : noop;
Expand All @@ -263,12 +266,14 @@
return updateInput(this, name, value, sanitizeFn);

unbind(this, name);
bindInputEvent(this, name, value, postEventFn);

var observable = value;
var binding = bindInputEvent(this, name, observable, postEventFn);
updateInput(this, name,
value.open(inputBinding(this, name, sanitizeFn)),
observable.open(inputBinding(this, name, sanitizeFn)),
sanitizeFn);

return this.bindings[name] = value;
return this.bindings[name] = binding;
}

HTMLTextAreaElement.prototype.bind = function(name, value, oneTime) {
Expand All @@ -281,11 +286,13 @@
return updateInput(this, 'value', value);

unbind(this, 'value');
bindInputEvent(this, 'value', value);

var observable = value;
var binding = bindInputEvent(this, 'value', observable);
updateInput(this, 'value',
value.open(inputBinding(this, 'value', sanitizeValue)));
observable.open(inputBinding(this, 'value', sanitizeValue)));

return this.bindings.value = value;
return this.bindings.value = binding;
}

function updateOption(option, value) {
Expand All @@ -304,8 +311,8 @@
option.value = sanitizeValue(value);

if (select && select.value != oldValue) {
selectBinding.setValue(select.value);
selectBinding.discardChanges();
selectBinding.observable_.setValue(select.value);
selectBinding.observable_.discardChanges();
Platform.performMicrotaskCheckpoint();
}
}
Expand All @@ -326,9 +333,12 @@
return updateOption(this, value);

unbind(this, 'value');
bindInputEvent(this, 'value', value);
updateOption(this, value.open(optionBinding(this)));
return this.bindings.value = value;

var observable = value;
var binding = bindInputEvent(this, 'value', observable);
updateOption(this, observable.open(optionBinding(this)));

return this.bindings.value = binding;
}

HTMLSelectElement.prototype.bind = function(name, value, oneTime) {
Expand All @@ -344,9 +354,12 @@
return updateInput(this, name, value);

unbind(this, name);
bindInputEvent(this, name, value);

var observable = value;
var binding = bindInputEvent(this, name, observable);
updateInput(this, name,
value.open(inputBinding(this, name)));
return this.bindings[name] = value;
observable.open(inputBinding(this, name)));

return this.bindings[name] = binding;
}
})(this);

0 comments on commit c3617b9

Please sign in to comment.