I love Backbone.js for many reasons: it’s lightweight, small (it’s the first open-source library whose code I’ve actually completely read), super simple to get started with and has a very active community.
Backbone Views leave a bit to be desired because they do very little. As the documentation itself says “Backbone views are almost more convention than they are code”.
One particular pain point I’ve had is the inability to manage collections of views. There already exist a number of solutions that provide you a nice way to build applications with more structure that what Backbone alone will give you. I decided to write Backbone ViewCollection for 2 reasons:
What should a View Collection Do?
These are the things I wanted from a View Collection class:
render
method that calls render
on all its child elementsdelegateEvent
and all the other crap associated with itHere is an example of what such a class would do:
var Pokemon = Backbone.Model.extend({});
var Pokedex = new Backbone.Collection({
model: Pokemon;
});
var PokemonView = Backbone.View.extend({
render: function() {
//some code to render a Pokemon's info
}
});
var PokedexView = new Backbone.ViewCollection({
childView: PokemonView,
collection: Pokedex
});
//This will create an instance of PokemonView for each model in
//the Pokedex collection and call its render() method
PokedexView.render();
In the coming blog posts, I will describe how I made my ViewCollection.