I’ve been spending the weekend learning Backbone.js and while building a simple app an issue came up that I don’t really understand.
I have a view that creates a collection and renders the collection’s view. To wit:
gallery: function(page,id) {
var that = this;
this.galleryCollection = new GalleryCollection;
this.galleryCollection.setID = id;
this.galleryCollection.region = page;
this.galleryCollection.fetch({add: true,
success: function(model,response) {
//render logic here
});
}
This works as expected; calling console.log(this.model.models) displays a collection with only the models associated with the above collection.
However, structuring the code like this:
gallery: function(page,id) {
this.galleryCollection = new GalleryCollection({setID: id, region: page})
this.galleryCollection.fetch()
//additional code...
adds the newly instantiated collection to the collection of models being fetched. I’m probably missing some fundamental Backbone.js paradigm here, but why would the collection itself be added to the collection?
The first argument to a
Backbone.Collectionconstructor is supposed to be an array of models to populate the collection with. It appears that you’re trying to set properties on the collection instance by passing them to the constructor. Perhaps that’s part of your problem.