Skip to content

Instantly share code, notes, and snippets.

@leomicheloni
Created May 31, 2014 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leomicheloni/cacd1cf00cd88a2fffd8 to your computer and use it in GitHub Desktop.
Save leomicheloni/cacd1cf00cd88a2fffd8 to your computer and use it in GitHub Desktop.
<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