js_di
JS Dependency Injection Container
Last updated 6 years ago by timglabisch .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install js_di -g
SYNC missed versions from official npm registry.

     #    "
  mmm#  mmm
 #" "#    #
 #   #    #
 "#m##  mm#mm

di is a simple and well tested javascript / coffeescript dependency injection container. it supports shared / non shared services and tags. it's written in a few lines of coffeescript, but feel free to copy the javascript source as well.

install

just grab the di.coffee or .js.

license

New BSD License

examples

default configuration

# some example classes
class a
  hello: -> "hello from a"

class b
  constructor: (@a) ->
  hello: -> "hello from b and " + @a().hello()

class demoSetterInjection
  setA: (@a) -> @
  hello: -> @a().hello()

i = 0

# create and configure the di
d = new di
d.configure
  factories:
    a: -> new a

    # constructor injection
    b: (di) -> new b di.getA

    # setter injection
    demoSetterInjection: (di) -> (new demoSetterInjection).setA di.getA

    notShared:
      shared: false
      factory: -> i++

    shared: -> i++

expect(d.getB().hello()).toBe "hello from b and hello from a"
expect(d.getDemoSetterInjection().hello()).toBe "hello from a"

# services are shared by default
expect(i).toBe(0)
d.getShared()
d.getShared()
d.getShared()
expect(i).toBe(1)

# there is also support for non-shared services
d.getNotShared()
d.getNotShared()
expect(i).toBe(3)


# HINTS
# getA returns a function that returns the service for laziness.
# getA is the same like get('a')
# getA() is the same like get('a')()

tags

d = new di
d.configure
  factories:
    pluginA:
      tag: "Plugin"
      factory: -> "a"
    pluginB:
      tag: ["Plugin", "you can have more than one tag :)"]
      factory: -> "b"
    somethingElse:
      -> "c"

# now get all "Plugins" :)
tags = d.getByTag('Plugin')
expect(tags[0]()).toBe "a"
expect(tags[1]()).toBe "b"

Current Tags

  • 1.0.4                                ...           latest (6 years ago)

5 Versions

  • 1.0.4                                ...           6 years ago
  • 1.0.3                                ...           6 years ago
  • 1.0.2                                ...           6 years ago
  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (4)
Dependents (2)

Copyright 2014 - 2017 © taobao.org |