jashkenas/backbone — pull request #2709 by marcgreenstock (2013-08-05 17:56:43+00:00)
Title: Dependency injection for views

Since the merging of options in views was removed in https://github.com/jashkenas/backbone/pull/2461 the views are missing certain functionality such as the ability to include multiple models/collections or required arguments.

The only way I can think of that would allow such functionality in the current state of backbone.js is to create a nested model with the required objects as attributes eg:

new ProductsView({
model : new Backbone.Model({
products : new Products,
session : new Session
})
});

This is less than ideal since the above example is instantiating a model with the single purpose of including more than one object to the view.

This pull request adds the ability to define expected dependencies and have them injected onto the instance:

ProductsView = Backbone.View.extend({
dependencies : ['session']
});

new ProductsView({
model : new Products,
session : new Session
});

This is loosely based off an idea @wookiehangover gave my co-developers, I believe he also gave a talk about this subject recently too.