I have a Backbone.js Collection and I have an array of model IDs that I want to populate it. I know that I could fetch these objects one by one, build an array of objects and pass them into the Collection’s constructor as an array.
What I’d like to be able to do is pass the array of object ids into the constructor as initial data, and get the Collection to fetch them, possibly as batch, as per this.
Doable?
When you call ‘fetch’ on a Backbone.Collection, it in turn calls Backbone.sync, which by default just asks the collection for the url to use.
So if your server responds to:
You could do something like:
And use it like so:
You’d have to pass the ids in the options parameter, because the Backbone.Collection constructor function sets the models passed in the first parameter before it calls the ‘initialize’ function.
Theoretically, this should work (read: completely untried).