Last active
August 29, 2015 14:18
-
-
Save gautamarora/6f98b5473e0862cadb0d to your computer and use it in GitHub Desktop.
Learning Backbone
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
var todo = new Todo({text:"learning backbone"}); //creates new todo | |
todo.toJSON() //logs todo as json object | |
JSON.stringify(todo) //logs todo as json string | |
todo.get("text") //get value of attribute | |
todo.set({done: true}) //set value of attribute. note: this fires 'todo:change:done' & 'todo:change' |
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
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Backbone</title> | |
</head> | |
<body> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js"></script> | |
<script> | |
var Todo = Backbone.Model.extend({ | |
defaults: { | |
text: '', | |
done: false | |
}, | |
validate: function(attrs) { | |
console.log("Todo:validate"); | |
}, | |
initialize: function() { | |
console.log("Todo:init"); | |
this.on('change', function() { | |
console.log('Todo:change'); | |
}) | |
this.on('change:done', function() { | |
console.log('Todo:change:done'); | |
}) | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment