-
-
Save lili21/18c6e1d801f1e59d05ff to your computer and use it in GitHub Desktop.
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