Created
May 31, 2014 20:57
-
-
Save leomicheloni/cacd1cf00cd88a2fffd8 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="js/vendor/jquery/jquery.min.js"></script> | |
<script type="text/javascript" src="js/vendor/underscore/underscore.js"></script> | |
<script type="text/javascript" src="js/vendor/backbone/backbone.js"></script> | |
</head> | |
<body> | |
<div id="container" ></div> | |
<script type="text/javascript"> | |
//creamos el modelo extendiendo la clase Model de backbone | |
var Task = Backbone.Model.extend({ | |
//valores por defecto de algunas propiedades | |
defaults:{ | |
done: false | |
}, | |
//método de validación, si retorna algo indica que ocurrió un error | |
validate: function(attributes){ | |
if(attributes.name.length===0) return "the name is required"; | |
} | |
}); | |
//creamos un objeto a partir de la definición del modelo | |
var task = new Task({name: "hacer algo"}); | |
//le asignamos un valor a la propiedad name | |
task.set("name", ""); | |
//escuchamos el evento change del modelo | |
task.on("change", function(){ | |
console.log("algo cambió"); | |
console.log("el valor anterior fue:"); | |
console.log(this.changed); | |
}); | |
//agregamos un filtro al evento para indicar que nos interesa el name | |
task.on("change:name", function(){ | |
console.log("el nombre cambió"); | |
}); | |
//si hay un error de validación cuando | |
//hacemos set("propiead", "valor", {validate: true} se ejecuta este evento | |
//también se ejecuta consultamos el método isValid() y existen errores | |
task.on("invalid", function(){ | |
console.log("ocurrió un error de validación"); | |
console.log(this.validationError); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment