Created
March 15, 2013 06:35
-
-
Save h2ospace/5167895 to your computer and use it in GitHub Desktop.
Backbone: Model
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
(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