inversify / InversifyJS

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

ES7 Decorator Examples #29

Closed amcdnl closed 8 years ago

amcdnl commented 8 years ago

Some ES7 Decorator examples would be awesome!

It would also be nice to see how u could take advantage of meta-data proposal too - https://github.com/rbuckton/ReflectDecorators

remojansen commented 8 years ago

Hi guys! using decorators and reflect-metadata is in my plans :+1: Thanks for your feedback and I promise I will do something about it as soon as I can :smile:

Jameskmonger commented 8 years ago

Fixed by #34 ? @remojansen

remojansen commented 8 years ago

Hi @amcdnl I'm closing this issue because as @Jameskmonger said above #34 has added support for the @Inject decorator:

You should be able to do the following:

TypeScript:

import { Inject } from "inversify";

@Inject("FooInterface", "BarInterface")
class FooBar implements FooBarInterface {
  public foo : FooInterface;
  public bar : BarInterface;
  public log(){
    console.log("foobar");
  }
  constructor(foo : FooInterface, bar : BarInterface) {
    this.foo = foo;
    this.bar = bar;
  }
}

JavaScript:

var FooBar = (function () {
  function FooBar(FooInterface, BarInterface) {
    this.foo = FooInterface;
    this.bar = BarInterface;
  }
  FooBar.prototype.log = function () {
    console.log("foobar");
  };
  return Inject("FooInterface", "BarInterface")(FooBar);
})();

Also, we have plans to use the reflec-metada API in 2.0 but we have not used for the moment because we don't really need it and we would need to use fallback and that would increase the size of InversifyJS.

Thanks for your feedback :+1:

amcdnl commented 8 years ago

Just to clarify, your not planning to support ES-next decorators?

remojansen commented 8 years ago

Yes we are going to use the ES7 Reflect API and some decorators. The decorators that I have in mine can be preview at https://github.com/inversify/InversifyJS/tree/2.0/source/decorators

One of them (@tagged) allows the users to add metadata that can be used in constrained bindings. Under the hood this decorators will use the Reflect to read and write the metadata. This is really similar to @Reflect.metadata(metadataKey, metadataValue) from the project that you shared (ReflectDecorators).

amcdnl commented 8 years ago

:) ... eta on release?