Created
August 10, 2017 21:23
-
-
Save Ollie-w/ab562175a39dda8e37fd4ce0078440f4 to your computer and use it in GitHub Desktop.
two way data binding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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