Skip to content

Instantly share code, notes, and snippets.

@bergie
Created December 20, 2012 15:31
Show Gist options
  • Select an option

  • Save bergie/4345959 to your computer and use it in GitHub Desktop.

Select an option

Save bergie/4345959 to your computer and use it in GitHub Desktop.
Collections handling example with Backbone
var router = Backbone.Router.extend({
initialize: function (options) {
this.collections = [];
},
routes: {
'new': 'newColl',
'show/:id': 'showColl'
},
newColl: function () {
var collection = this.createCollection();
var view = new NewCollView({
collection: collection
});
view.render();
},
showColl: function (id) {
var collection = this.getCollection(id);
var view = new ShowCollView({
collection: collection
});
view.render();
}
// Helpers
getCollection: function(id) {
return this.collections[id];
},
createCollection: function(options) {
var newCollection = new Collection(options);
this.collections.push(newCollection);
newCollection.id = this.collections.indexOf(newCollection);
return newCollection;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment