backbone-di
v0.1.0
Published
Backbone dependency injector
Downloads
5
Readme
backbone-di
Installing backbone-di
The package can be installed through npm or bower.
Using npm
npm install backbone-di
Using bower
bower install backbone-di
After installing the package, you can grab the backbone-di.js
or the minified version of it backbone-di.min.js
Usage examples
Bindings
// Create an instance of Backbone.Injector
var injector = new Backbone.Injector();
// Bind a value which can be of any type, returns the string R. Moorman
injector.bind('name').toValue('R. Moorman');
// Bind a type which returns every time a new instance
injector.bind('myapp:button').toType(Backbone.View);
// Bind a type to a singleton which returns only one instance of it
injector.bind('myapp:config').toSingleton(Backbone.Model.extend({
defaults: {
url: 'https://github.com/moorinteractive/backbone-di'
}
}));
Injections
// Create an instance of Backbone.Injector
var injector = new Backbone.Injector();
// Let's do some bindings...
injector.bind('myapp:config').toValue({
title: 'Ahoy matey!'
});
// Create a new Backbone.View with the config injected
var view = new (Backbone.View.extend({
injections: {
config: 'myapp:config'
}
}));
injector.inject(view);
// Retrieve the config title by the key which is determined in the
// config, in this case that will be 'config'.
var title = view.config.title;
Note that the key in the config of injections
will be the property name
under which the instance is injected.
Changelog
0.1.0
- Prototype