Small Backbone extension for injector.js dependency injection


410.1.53 years ago6 years agoMinified + gzip package size for @gigwalk/backbone-injector in KB


backbone-injector Build Status

Automatic dependency injection within all Backbone.View instances. This is build on top of Backbone and injector.js


You could just download backbone-injector.js or backbone-injector.min.js from the project root.

We advise you to download it using Bower instead:

bower install backbone-injector

This command will automatically download all dependencies. So in this case it takes care of downloading injector.js, jQuery, Backbone and Underscore for you.


To use the dependency injection, you need to pass an instance of the injector as the injector argument while instantiating a view. This plugin will take care of the boilerplate and automatically injects the view before the initialize method is called.

See for example the following code stemming from a Jasmine test:

var model = new Backbone.Model();
var injectorInstance = new injector.Injector();

var ViewClass = Backbone.View.extend({
    model: 'inject'

var view = new ViewClass({injector:injectorInstance});


Under the hood the Backbone.View is adjusted prototype and make sure the dependency injection takes place before the initialize method is called. Thus it is safe to rely on the view to be injected once you start using it.

We also inject the injector inside each view. This is for convenience, so you can easily pass it on to other view instances.

var injectorInstance = new injector.Injector();
var view = new ViewClass({injector:injectorInstance});


For example usage of this library, please refer to Navigator-Injector-Backbone-Command-TodoMVC example.


Feel free to create a new issue for all your questions, issues or feature requests.

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.