Sync an entire collection of models in backbone.js

OK, a fresh pot of coffee and a day later, I got it. We were calling sync() improperly. Here is what we should have been doing:

syncCollection: function() {
        Backbone.sync('create', this);
    }

Thanks to machineghost for helping me explore this problem - I was totally focused on the wrong thing.


The reason you are getting that error is that Backbone.Collection does not have a save method (its models do). What I think you're looking for is:

syncCollection: function() {
    _(this.models).each(function(model) {
        model.save();
    });
}

Or better yet, using Collection's each:

syncCollection: function() {
    this.each(function(model) {
        model.save();
    });
}

Or better still, using Collection's invoke:

syncCollection: function() {
    this.invoke('save');
}

However, I'm not sure you even need any of that, as Backbone.Collection already has a method that does (I think) what you want: sync.

From the Backbone docs:

collection.sync(method, collection, [options])

Uses Backbone.sync to persist the state of a collection to the server. Can be overridden for custom behavior.