views:

366

answers:

4

Are there any inversion of control frameworks for javascript?

The closest answer available on stackoverflow that I could find is here: http://stackoverflow.com/questions/619701/wiring-code-in-javascript . 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.

+2  A: 

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

Luke Schafer
Can you elaborate on why you did not need it?
Frank Schwieterman
Well, as I said in the other comment, most people use service locators for this sort of need in JS. Additionally, The size of your JS really does matter, so unless you're making a _really_ thick client, you want to minimise as much code-size and computation as possible. See http://video.yahoo.com/watch/1041101/3881103
Luke Schafer
+2  A: 

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}
};
Kevin Hakanson
Yeah I wasn't a fan of squirrel either, hence writing my own. I had the same thing - started it but didn't have a real need for it so never finished. Take a look at the source of mine, especially the unit tests, there's a lot more you can do with it than the site ways.
Luke Schafer
Can you elaborate on why you did not need it?
Frank Schwieterman
+2  A: 

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.

Rene Saarsoo
I think DI is different from IoC. Yes DI is easy in Javascript as you can write where ever you want, I am using DI currently for unit testing. Although DI is much easier in Javascript, I think IoC could still have advantages in documenting dependencies and in making test code cleaner. Right now when I test something I have to manually verify every dependency has been properly replaced. With IoC you can clearly see what dependencies exist and what they've been replaced with.
Frank Schwieterman
Good point Frank.
Rene Saarsoo
I'm just passing through so I don't have time to look up a reference, but DI is not just for testing (it's not even the main benefit).
Luke Schafer
+1  A: 

I put together a simple lightweight ioc container, called it JsfIoc.

http://github.com/fschwiet/JsfIoc

Frank Schwieterman