npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

javascript-dependency-injection

v1.0.2

Published

Javscript dependency injection library

Downloads

15

Readme

Get this with Bower

Build Status Coverage Status Dependency Status devDependency Status Code Climate Inline docs

Gitter

Javascript Dependency Injection library

DI makes classes accessible by a contract. Instances are created when requested and dependencies are injected into the constructor, facilitating lazy initialization and loose coupling between classes.

As an example, consider a User and Persitance classes:

function WebSql(name, fieldList)  {
    this.persist = function (obj) {
        console.log('WebSQL will persist:');
        fieldList.forEach(function (field) {
            console.log('    ' + field + ': ' + obj[field]);
        });
    }
}

function IndexDB(name, fieldList)  {
    this.persist = function (obj) {
        console.log('IndexDB will persist:');
        fieldList.forEach(function (field) {
            console.log('    ' + field + ': ' + obj[field]);
        });
    }
}

function User(email, passwd, storage, role) {    // the `storoge` parameter holds an instance
    this.email = email;
    this.passwd = passwd;
    this.role = role;

    this.save = function () {
        storage.persist(this);
    };
}

With these classes in our pocket its time to setup the relations between them. The function that does this has the following signature

 function (<contract name>, 
           <class reference>, 
           [optional list of constructor arguments], 
           {optional configuration object} ) 
           

Or just in code:

var di = new DI();

di.register('user', User, [null, 'welcome', 'websql', 'nobody']);
di.register('websql', WebSql, ['userTable', ['email','passwd', 'role']], {singleton: true});
di.register('indexdb', IndexDB, ['userTable', ['email','passwd', 'role']], {singleton: true});
      

Note that the provied constructor arguments are default values or contract names. From now it is easy to create instances:

var user1 = di.getInstance('user', ['[email protected]']),
        -> email: '[email protected]', passwd: 'welcome', storage : WebSQL instance, role: 'nobody'
    user2 = di.getInstance('user', ['[email protected]', 'newSecret']); // define a new password
        -> email: '[email protected]', passwd: 'newSecret', storage : WebSQL instance, role: 'nobody'
        
if (user1 instanceof User) { /* user1 is an instance of User!! */ }

But it is also possible to use IndexDB as the persistance class:

var user = di.getInstance('user', ['[email protected]', null, 'indexdb']),               // The password is set to null too!
        -> email: '[email protected]', passwd: null, storage : IndexDB instance, role: 'nobody'
    root = di.getInstance('user', ['[email protected]', undefined, 'indexdb', 'admin']); 
        -> email: '[email protected]', passwd: 'welcome', storage : IndexDB instance, role: 'admin'
        

Checkout more detailed the documentation here

Gulp tasks

Install the dependencies as follows

$> npm install

To minify the library

$> gulp

To run the tests

$> gulp test

Installation

$> bower install javascript-dependency-injection