Skip to content

Instantly share code, notes, and snippets.

@lili21
Forked from euforic/dataBind.js
Last active August 29, 2015 14:07
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 lili21/18c6e1d801f1e59d05ff to your computer and use it in GitHub Desktop.
Save lili21/18c6e1d801f1e59d05ff to your computer and use it in GitHub Desktop.
/**
* two way data binding
*/
// create root scope
window.$rootScope = {};
function twoWayBind(el){
var name = el.id;
$rootScope[name] = null;
// watch object scope for changes
$rootScope.__defineSetter__(name, function(e){
this['_' + name] = e;
// TODO add interpolation
el.innerHTML = e;
});
$rootScope.__defineGetter__(name, function(e){
return this['_' + name];
});
// watch bound el for changes
el.addEventListener('change', function(e){
$rootScope['_' + name] = e.srcElement.value;
console.log($mainScope);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment