Skip to content

Instantly share code, notes, and snippets.

@euforic
Created March 6, 2013 00:31
Show Gist options
  • Save euforic/5095711 to your computer and use it in GitHub Desktop.
Save euforic/5095711 to your computer and use it in GitHub Desktop.
two way databinding example implementation
/**
* 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