|
JasFac
JavaScript Agile Suite - IoC/DI Container
IntroductionJasFac 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. DetailsJasFac allows you to:
ExamplesSample 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-timeResolving 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