Coder Social home page Coder Social logo

backbone-injector's Introduction

backbone-injector Build Status

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

Install

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.

Usage

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();
injectorInstance.map('model').toValue(model);

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

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

expect(view.model).toEqual(model);

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});

expect(view.injector).toEqual(injectorInstance);

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

Support

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

backbone-injector's People

Contributors

paultondeur avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.