Skip to content

Instantly share code, notes, and snippets.

@Ollie-w
Created August 10, 2017 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ollie-w/ab562175a39dda8e37fd4ce0078440f4 to your computer and use it in GitHub Desktop.
Save Ollie-w/ab562175a39dda8e37fd4ce0078440f4 to your computer and use it in GitHub Desktop.
two way data binding
function bindValue(objectToBind) {
var elemToBind = document.getElementById(objectToBind.id)
elemToBind.addEventListener("change", function() {
objectToBind.value = this.value;
})
}
function proxify(id) {
var handler = {
set: function(target, key, value, receiver) {
target[key] = value;
document.getElementById(target.id).value = value;
return Reflect.set(target, key, value);
},
}
return new Proxy({id: id}, handler);
}
var myObject = proxify('element-to-bind')
bindValue(myObject);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment