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