Skip to content

Instantly share code, notes, and snippets.

@acazsouza
Created April 8, 2015 19:26
Show Gist options
  • Save acazsouza/66d66c6ad08504164fca to your computer and use it in GitHub Desktop.
Save acazsouza/66d66c6ad08504164fca to your computer and use it in GitHub Desktop.
<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