Skip to content

Instantly share code, notes, and snippets.

@KrisJordan
Created February 6, 2011 04:58
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 KrisJordan/813138 to your computer and use it in GitHub Desktop.
Save KrisJordan/813138 to your computer and use it in GitHub Desktop.
What is Backbone's idiomatic way to create/save valid models?
Backbone = require 'backbone'
Backbone.sync = ->
console.log 'Sync called.'
Book = Backbone.Model.extend
validate: ->
console.log 'Validate called.'
return "error"
Library = Backbone.Collection.extend
model: Book
lib = new Library
lib.create
title: 'A Book'
# Skips validate
(new Book { title: 'Another book.'}).save()
# Skips validate
book = new Book
book.save
title: 'A third book.'
# Runs validate, does not sync
@KrisJordan
Copy link
Author

Was surprised that save() without attrs bypasses validate. Is the recommended approach not setting with the constructor, but instead setting with set/save? Is there any way to enforce validation on Collection's create method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment