hannahhoward / a1atscript

The Angular 2 Polyfill
MIT License
101 stars 7 forks source link

Using the 'appInjector' property in @Component as an alias to adding module deps #19

Closed timkindberg closed 9 years ago

timkindberg commented 9 years ago

The latest Angular 2 has renamed injectables to appInjector. See: https://angular.io/docs/js/latest/api/annotations/ComponentAnnotation-class.html.

In addition to renaming this, I think we should consider allowing appInjector to take not only string-based ng1 injectables (e.g. '$http') but also a1atscript created injectables (e.g. Greeter).

For example, currently we do this:

@Service('greeter')
class Greeter {}

@AsModule('GreetCmpt', [Greeter])
@Component({
  selector: 'greet',
  injectables: ['$http', 'greeter']
})
class HelloWorld {
  constructor($http, greeter) {
    this.greeter = greeter;
  }
}

We have to add the Greeter service as a module dependency on the Greet component's module. Then also add it to injectables. Seems like we could reduce some boilerplate here.

We should be able to do this:

@Service
class Greeter {}

@Component({
  selector: 'greet',
  appInjector: ['$http', Greeter]
})
class HelloWorld {
  constructor($http, greeter) {
    this.greeter = greeter;
  }
}

In this example I've renamed injectables to appInjector but I've now added the ability to not only inject string-based things like '$http' but also the Greeter service. This more closely matches how ng2 uses classes instead of strings. Also, notice I left out the @AsModule because it should be able to grab the module off of the Greeter class passed into the appInjector.

So the acceptance criteria for this feature would be:

  1. Rename injectables to appInjector
  2. Allow passing a1atscript class-based injectables in addition to string-based regular ng1 injectables
  3. Add the modules from a1atscript classes in appInjector to the dependencies of the auto-generated module of the host class.
  4. If @AsModule is present then merge the dependencies, also allow @AsModule to only specify a name and no dependencies, e.g. @AsModule("GreetComponent")