Last active
August 29, 2015 13:57
-
-
Save leomicheloni/9875159 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"> | |
//declaramos el modelo | |
var Tarea = Backbone.Model.extend({ | |
defaults:{ | |
done: false | |
} | |
}); | |
//declaramos la colección | |
var Tareas = Backbone.Collection.extend({ | |
model: Tarea, //indicar el tipo de modelo a la colección | |
//permite agregar elementos como json | |
getDone: function(){ //agregamos un método que retorne | |
//las tareas terminadas | |
return this.where({done: true}); //el método where es de la clase base | |
} | |
}); | |
//creamos una colección y le pasamos un array de modelos | |
//como declaramos el tipo de modelo backbonejs se encarga | |
//de generar cada uno sin nuestra ayuda | |
var tareas = new Tareas( | |
[ | |
{"name": "comprar lechuga"}, | |
{"name": "llamar a Roger"}, | |
{"name": "ir a nadar"} | |
] | |
); | |
tareas.add({name: "ir al chino"}); | |
tareas.on("change", function(){ | |
console.log("algo ha cambiado"); | |
}); | |
console.log(tareas.get({cid: "c3"})); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment