-
-
Save acazsouza/66d66c6ad08504164fca 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
<html> | |
<head> | |
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.min.js"></script> | |
<script> | |
var viewModel = {}; | |
var MVVMFramework = {}; | |
MVVMFramework.Init = function (viewModel) { | |
var $els = $('*[data-bind="Nome"]'); | |
var _onChange = function ($e) { | |
var propName = $e.attr('data-bind'); | |
var val = $e.val(); | |
viewModel[propName] = val; | |
} | |
$els.each(function () { | |
var $e = $(this); | |
$e.change(function () { | |
_onChange($e); | |
}) | |
}) | |
var _updateView = function (propName, val) { | |
$('*[data-bind="' + propName + '"]').val(val); | |
} | |
for (var propName in viewModel) { | |
viewModel.__defineGetter__(propName, function () { | |
return this['_' + propName]; | |
}); | |
viewModel.__defineSetter__(propName, function (val) { | |
this['_' + propName] = val; | |
_updateView(propName, val); | |
}); | |
} | |
} | |
//Aqui começa o uso do framework | |
window.onload = function () { | |
viewModel = { | |
Nome: 'teste' | |
} | |
MVVMFramework.Init(viewModel); | |
viewModel.Nome = 'Acaz'; | |
viewModel.Nome = 'Testando 123'; | |
} | |
</script> | |
</head> | |
<body> | |
<input type="text" data-bind="Nome" /> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment