Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Backbone add models to collection (Titanium Alloy or any Backbone Collection)
// Often you want to dump a large amount of models into a collection at one time.
// This take models from one collection and adds thems to another
// This assumes you have two collections already initialized with models.
// If you had one collection that had change events wired to render a view you might use this.
collectionAttachedToView.add(collectionToAdd.models);
//This would work, however, it will fire an add event everytime one of the models is added.
// This will slow your app down ad it will try to render over and over again as many times as the models you are adding
// To avoid this, use the silent option
collectionAttachedToView.add(collectionToAdd.models, {silent: true});
//Then fire an add event once so that it rerenders only once
collectionAttachedToView.trigger("add");
// This is very helpful in pagination. I was creating an Alloy app for the Iphone in Titanium and I was getting results from a database
// I wanted to create the models before I added them to the collection that was attached to the listView
// It was so slow as it was rerending the listview everytime a model was added.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment