Created
December 20, 2012 15:31
-
-
Save bergie/4345959 to your computer and use it in GitHub Desktop.
Collections handling example with Backbone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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