Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

injector-js

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

injector-js

Simple Dependency Injection Container for JavaScript

  • 0.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

InjectorJS

InjectorJS is a simple Dependency Injection Container for JavaScript. It helps you to remove hard-corded dependencies and make it possible to change them at any time. For more information about this pattern look at:

  • http://en.wikipedia.org/wiki/Dependency_injection
  • http://en.wikipedia.org/wiki/Inversion_of_control
  • http://www.youtube.com/watch?v=IKD2-MAkXyQ

Documentation

This docs describe many features of the library, but not all. Use source to learn InjectorJS better.

###Installation###

For installation read related chapter.

###Usage###

Online working version of fol;owing examples is available on plunker: http://plnkr.co/edit/SDXdO5. Feel free to play around with it!

Definition of simple objects (parameters):

console.log(injector.has('name')); // false

injector.set('name', 'Bob');
injector.set('PI', 3.14).set('currentTime', new Date());

console.log(injector.has('name')); // true

Defition of objects by factory functions:

injector.set('auto', function() {
  return {
      color: this.get('auto_color'),
      type:  this.get('auto_type')
});

Factory functions has access to current injector making it possible to references to other objects (inject dependencies). Objects are created only when you require them. These cause to that order of the definitions does not matter and there is no performance penalty.

You can define several objects in one set method call:

injector.set({
    auto_color: function() {
        return Math.random() < 0.5 ? 'black' : 'white';
    },
    auto_type: function() {
        return Math.random() < 0.5 ? 'hatchback' : 'sedan';
    },
    auto: function() {
        return {
            color: this.get('auto_color'),
            type:  this.get('auto_type')
        };
    }
});

You can add object factory to injector to simplify creation of common type objects. Before creation object value will be passed to appropriate object factory:

injector.setFactory('x60', function(value) {
    return value*60;
});

injector.set('hours_in_minutes', 'x60', function() {
    return 5;
});

console.log(injector.get('hours_in_minutes')); // 300

injector.setFactory('join', {
    create: function(strings) {
        return strings.join(' ');
    }
});

injector.set('hello', 'join', ['hello', 'world', '!']);

console.log(injector.get('hello')); // hello world !

As you see object factory can be a function or an object with create method. By default there are 3 object factories:

  • Object Factory - default factory - just return object;
  • Service Factory - create instances of classes;
  • Clazz Factory - create clazzes (see ClazzJS)
injector.set({
    service: {
        person: function() {
            return {
                class: this.get('person_class'),
                init:  [this.get('person_age')],
                call: {
                    setName: [this.get('person_name')]
                }
            }
        }
    },
    clazz: {
        person_class2: function() {
            return {
                name:   'Some/Clazz/Name',
                parent: 'Some/Clazz/Parent',
                deps: [10,20,30]
            }
        }
    },
    person_class: function() {
        
        var Person = function(age) {
            this.name = undefined;
            this.age  = age;
        };
        Person.prototype.setName = function(name) {
            this.name = name;
        }
        
        return Person;
    },
    person_age: 10,
    person_name: 'Bob'
});

var person = injector.get('person');

console.log(person.name); // 'Bob'
console.log(person.age);  // 10

Dependencies

License

Copyright (c) 2013 Aleksey Podskrebyshev. Licensed under the MIT license.

githalytics.com alpha

Bitdeli Badge

Keywords

FAQs

Package last updated on 19 Jan 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc