hannahhoward / a1atscript

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

Add some way to mimick the various dependency injections in ng2 (@Parent, @Query, etc) #22

Open timkindberg opened 9 years ago

timkindberg commented 9 years ago

See: https://angular.io/docs/js/latest/api/annotations/DirectiveAnnotation-class.html. There are various dependencies you can inject into the controller class constructor.

  1. directive:DirectiveType: a directive on the current element only
  2. @Ancestor() directive:DirectiveType: any directive that matches the type between the current element and the Shadow DOM root. Current element is not included in the resolution, therefore even if it could resolve it, it will be ignored.
  3. @Parent() directive:DirectiveType: any directive that matches the type on a direct parent element only.
  4. @Query(DirectiveType) query:QueryList<DirectiveType>: A live collection of direct child directives.
  5. @QueryDescendants(DirectiveType) query:QueryList<DirectiveType>: A live collection of any child directives.

In angular 1:

Thoughts on 4 & 5: Perhaps an api like this would be possible, where we auto-generate a $Query method on every component class that would help with this:

@Component({ selector: 'child-a' })
class ChildA {
  foo() { console.log('foo') }
}

@Component()
@View({
  inline: `
    <child-a/>
    <child-a/>
  `
})
class MyComponent {
  constructor() {
    let childrenA = this.$Query(ChildA);
    console.log(childrenA.length); // outputs 2
    console.log(childrenA[0].foo()); // outputs 'foo'
  }
}
hannahhoward commented 9 years ago

Hey looking into this -- one challenge is the Component class you define functions internally as the NG1 Directive's controller. And in fact, required controllers in NG1 aren't injected till the link function :(. I'm still looking into how to work around this but it may require going to some internal NG1 private api's.

timkindberg commented 9 years ago

Yeah we actually had some issue using $element in the class because of that. The child components were not ready yet.

0x-r4bbit commented 9 years ago

Quick note: @Parent is obsolete. Parent dependencies are unbound by default. In other words, if we ask for a dependency in a component it simply looks it up in the dependency injector tree until it finds one. @Host is then used to restrict the lookup to the component's host. Not sure if that's something you want to support for isolated scope directives.

timkindberg commented 9 years ago

@PascalPrecht we are looking into some of these other things over at the new ng-forward project. https://github.com/ngUpgraders/ng-forward

0x-r4bbit commented 9 years ago

So this repo here is deprecated?

timkindberg commented 9 years ago

No. The other one is not ready and definitely takes learnings from all of the existing 'ng2 polyfill' repos, but I could see each living on in its own way. We still use a1atscript at work.

0x-r4bbit commented 9 years ago

alright gotcha.

On Mon, Aug 31, 2015 at 4:29 PM, Tim Kindberg notifications@github.com wrote:

No. The other one is not ready and definitely takes learnings from all of the existing 'ng2 polyfill' repos, but I could see each living on in its own way. We still use a1atscript at work.

— Reply to this email directly or view it on GitHub https://github.com/hannahhoward/a1atscript/issues/22#issuecomment-136371352 .

/pp