Reputation: 24480
Are there any inversion of control frameworks for javascript?
The closest answer available on stackoverflow that I could find is here: What is the right way to wire together 2 javascript objects? . It looks like a great start, but I thought I'd be able to find something with a longer development history.
I've only used Castle Windsor myself, and I am really missing it in web-client land.
Upvotes: 16
Views: 7881
Reputation: 1201
Try Infusion. It is a pretty powerful IoC JS framework. It is used by couple of research centers in universities of Toronto and Berkeley Infusion
GitHub page of the project with more information could be found here Infusion GitHub pages
Upvotes: -1
Reputation: 6790
Try canDI. It's a simple dependency injection and object creation library. You can create singletons, instances and variables that are automatically registered at creation.
https://github.com/bflemi3/canDI
Upvotes: 0
Reputation: 24979
I have create and IoC container for JavaScript apps please check it out at http://blog.wolksoftware.com/introducing-inversifyjs
Upvotes: 1
Reputation: 514
We built a simple JavaScript IoC container called hilary.js: https://github.com/Acatar/hilaryjs.
With hilary, you can register and resolve services and factories. It supports child containers too, if you need or want to scope your containers.
EDIT I added some examples using hilary, as well as an example of achieving Dependency Injection with require.js:
Upvotes: 0
Reputation: 1849
Check out Inverted http://philmander.github.com/inverted/, a feature packed Javascript IOC container I have created. It runs on top of AMD in the browser and also works with Node.
Used in conjunction with AMD, Inverted uses a separate configuration file to express how classes are instantiated and how they interact. Once these defaults and relationships have been defined, an application context can be created, and instances of the classes can be used.
http://dailyjs.com/2013/01/04/controldeck-xlsx-inverted/
Upvotes: 1
Reputation: 24480
Another (newer) option is requireJS (http://requirejs.org/).
Upvotes: 0
Reputation: 11064
You could look at this simple library: fcjs It's very small but can be powerful in decoupling your code. It's inspired by Swiz AS3 framework
Upvotes: 0
Reputation: 15336
I use one, here's simple code from specs (it's CoffeeScript):
di.register 'a', -> 'component a'
di.get('a').should be: 'component a'
There are also callbacks, different scopes (application, instance, custom), ability to explicitly assign components.
DI: https://github.com/alexeypetrushin/rad_core/blob/master/assets/lib/dependency_injection.coffee
I use it to assemble Backbone.js application, there are lots of objects (App, Menu, Notice, ...) and it's make my life easier.
WARN: I use it internally with altered native objects, so there may be some errors :) Please let me know about them, I probably fix it in a day or two (by submitting via issues page https://github.com/alexeypetrushin/rad_core/issues ).
P.S. Don't like the IoC term it's too broad, DI is much more exact definition.
Upvotes: 0
Reputation: 24480
I put together a simple lightweight ioc container, called it JsfIoc.
http://github.com/fschwiet/JsfIoc
Upvotes: 3
Reputation: 13917
In dynamically typed languages like JavaScript and Ruby, DI isn't really that beneficial.
The main benefit of DI in statically typed languages like Java is in testing - to replace the real implementation of some class with a mock. That's because in Java classes are immutable and you can't just that easily replace them with mocks - you need a whole DI system to accomplish that.
But in JavaScript you can easily replace existing classes/methods with mocked ones. So DI is not really needed to achieve testability.
Of course there are other scenarios where DI might be useful, but you haven't really pointed out what you want to use the DI for, so I covered the most obvious case.
Upvotes: 0
Reputation: 42210
I was looking for one last year and ran across squirrel-ioc. There was something I didn't like about it - I think it only supported singleton style instances.
Squirrel is an IoC container implemented in Javascript to promote the better use of architecture and patterns in browser-based Javascript applications
I started writing my own and got pretty far (constructor and setter injection, values and reference relationships, singleton support, JsUnit tests) but never really needed it in my application. I may have to check out Luke's project. For reference, here is an example of the configuration format I ended up with.
var iocConfig = {
"a" : { Type : A },
"b1" : { Type : B, Props : [{Name : 'Letter', Ref : "a"}] },
"b2" : { Type : B, Props : [{Name : 'Letter', Val : "a"}] },
"c2" : { Type : C, Args : [{Ref : "a"}, {Val : "a"}] },
"d" : { Type : D, Props : [{Name : 'Letter', Ref : "a"}] },
"date" : { Type : Date, Props : [{Name : 'FullYear', Val : 2008}, {Name : 'Month', Val : 0}, {Name : 'Date', Val : 1}] },
"array3" : { Type : Array, Args : [{Val : 3}] },
"number1" : { Type : Number, Args : [{Val : 1}] },
"string1" : { Type : String, Args : [{Val : "1"}] },
"s-true" : { Type : S, Singleton : true},
"s-false" : { Type : S, Singleton : false}
};
Upvotes: 4
Reputation: 9265
I started writing one that I never got around to finishing. Not sure if I ever will as the overhead probably isn't worth it. if you're interested, it's at: http://code.google.com/p/jasproject/wiki/JasFac (that's the IoC portion, the full suite is at http://code.google.com/p/jasproject/)
The mocking library is fairly complete (no expectations though, at the moment i just use assertions on the mocks/stubs) but the unit testing framework is lacking. The IoC portion is pretty complete but might have a few bugs (don't think so though)
Feel free to use it and/or contribute, I can help where you need.
EDIT: More usage can be seen in the unit tests for jasfac: https://jasproject.googlecode.com/svn/trunk/Jas.Tests/JasFacTests.js
Upvotes: 4