Skip to content

Instantly share code, notes, and snippets.

@h2ospace
Created March 15, 2013 06:35
Show Gist options
  • Save h2ospace/5167895 to your computer and use it in GitHub Desktop.
Save h2ospace/5167895 to your computer and use it in GitHub Desktop.
Backbone: Model
(function() {
// Model
var Task = Backbone.Model.extend({
defaults: {
title: 'do something!',
completed: false
},
validate: function(attrs) {
if (_.isEmpty(attrs.title)) {
return "title must not be empty!";
}
},
toggle: function() {
this.set('completed', !this.get('completed'));
}
});
var task1 = new Task({
completed: true
});
task1.set('title', 'newTitle');
var title = task1.get('title');
console.log(title);
console.log(task1.toJSON());
task1.toggle();
console.log(task1.toJSON());
task1.set({title: ''}, {validate: true});
console.log(task1.toJSON());
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment