My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
JasFac  
JavaScript Agile Suite - IoC/DI Container
Updated Feb 4, 2010 by Luke.Sch...@gmail.com

Introduction

JasFac is a powerful, light-weight Inversion of Control / Dependency Injection container for JavaScript.

The name 'JasFac' is a nod to Autofac, also found here on Google Code.

Details

JasFac allows you to:

  • Register components as singletons or transient
  • Register a component by providing a prototype
  • Register a component by providing an expression (a closure that returns the component)
  • Register a component by providing an instance (automatically flagged as singleton)
  • Autowire constructor arguments
  • Specify custom parameters for autowired constructors
  • Specify properties to be injected on resolution
  • Identify components by name, reference, or both
  • Resolve components with custom arguments
  • Resolve components with custom parameters

Examples

Sample container initialisation:

var builder = new JasFac.Builder();
var container = new JasFac.Container();

// register attempts to determine what you're registering
builder.register(
    function(c) { return new Foo(c.resolve("Bar")); }
).named("Foo");

// can explicitly register a constructor function. This will
// automatically call .as(), but you can also call .named()
builder.registerConstructor(Bar).injectArguments({
    arg1 : "value",
    arg2 : function(c) { c.resolve(FooBar); }
});

// set the scope
builder.register(Foobar)).named("Foobar").scoped(JasFac.Constants.SINGLETON);

// .injectProperties only works on non-initialised transients
builder.register(Foobar).named("Foobar").injectProperties({name: "override!"});

// After all registrations have been specified, build the container
// This will error if it detects required dependencies are missing
builder.build(container/*, true*/); // second arg is optional - specifies whether singletons should be initialised at build-time

Resolving is easy:

var foo = container.resolve("Foo");
var fooBar = container.resolve(FooBar);
var fooBarName = fooBar.name; // equals "override!" - this is injected as per the second registration of FooBar above

Sign in to add a comment
Powered by Google Project Hosting