Last active
February 10, 2017 13:03
-
-
Save frentsel/c902fcf4be2a99487e74 to your computer and use it in GitHub Desktop.
Simple 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
var Field = function(selector){ | |
var el = document.querySelector(selector), | |
callback; | |
this.get = function() { | |
return el.value; | |
}; | |
this.set = function(val) { | |
el.value = val; | |
}; | |
this.handler = function(_callback) { | |
callback = _callback; | |
}; | |
el.onchange = el.oninput = el.onpaste = function(){ | |
callback(el.value); | |
}; | |
}; | |
var input = new Field("input"); | |
input.set("Sanya"); | |
input.handler(console.log); | |
input.get(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment