inversify / InversifyJS

A powerful and lightweight inversion of control container for JavaScript & Node.js apps powered by TypeScript.
http://inversify.io/
MIT License
11.33k stars 718 forks source link

Inversify usage without TS #514

Closed sebastian-zarzycki-apzumi closed 7 years ago

sebastian-zarzycki-apzumi commented 7 years ago

I'm operating in React stack, using Babel (ES2015 preset and some stage-0 things, decorators, etc). I'm having a hard time figuring out how to inject properties. I am able to create container, specify bindings. mark class as @injectable and when pulling dependencies straight from the container, it's fine. But how do I specify ES6 class and mark its properties as injectable? I've tried @lazyInject, but also to no avail. I know that part of the issue is that ES6 class doesn't really have properties defined in other place than constructor, but I saw other decorators (namely: type definitions from pleerock/class-transformer) somehow getting away with it using this syntax:

class Something {

@customDecorator
myProperty = null; // or undefined

}

If I get this right, this syntax is then compiled to go into constructor anyway. (see example here)

I've seen some examples using vanillajs, but they all rely on injecting into constructor parameters, using index, which seems inelegant to say the least. There's no example using ES6 decorators or injecting property by name, not by index.

Is it possible to use decorators somehow to work around this, with @inject or @lazyInject?

remojansen commented 7 years ago

We need to add a feature for this. We have already an issue https://github.com/inversify/InversifyJS/issues/489. Please keep en eye on that issue to find the implementation progress.

sebastian-zarzycki-apzumi commented 7 years ago

Thanks for answer. My underlying question, though, was: how do I proceed for now, without this feature being implemented?

remojansen commented 7 years ago

I'm afraid that because React only works with @lazyInject and you are using babel this issue is a blocker. There is no way to get around it for now. I will try to implement that feature for you. I was going to pick a couple of tasks from the backlog this week so I will pick #489 first. I'm sorry but that is the best I can do to help you at the moment.

sebastian-zarzycki-apzumi commented 7 years ago

Thank you, I sure do appreciate this!