Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Directives should support module publishing. #652

Closed
mhevery opened this issue Feb 27, 2014 · 4 comments
Closed

Directives should support module publishing. #652

mhevery opened this issue Feb 27, 2014 · 4 comments
Milestone

Comments

@mhevery
Copy link
Contributor

mhevery commented Feb 27, 2014

When the directive is trigger its annotation should allow for modules which would be merged into the injector. In this way the directive could do more then just instantiate itself. It could also override instances, or create a whole collection of instances.

This should replace publishTypes annotation property.

We have a publishTypes https://docs.angulardart.org/#angular/angular.NgAnnotation@id_publishTypes annotation.

It is used like this

@NgDirective({
  publishTypes: [SomeInterface]
})
class SomeDirective implements SomeInterface {
}

The reason for this is that if we could have multiple directives which implement SomeInterface we would like for to inject it as SomeInterface. The issue is that when the module gets registered it will say something like this:

module.type(SomeDirective)

This means that asking for SomeInterface will return error. What the publishTypes property does is do an extra registration like so:

module.type(SomeDirective)
module.factory(SomeInterface, (i) => i.get(SomeDirective));

This enables one to ask for SomeInterface and get SomeDirective implementation.

But this is really just one specific use case. A more generic case is that a Directive should be able to create a custom module definition which should be loaded into the injector. Then the directive can performa a lot more.

Something like so:

@NgDirective({
  module: SomeDirective.module;
})
class SomeDirective implements SomeInterface {
  static _module = new Module()
        ..factory(SomeInterface, (i) => i.get(SomeDirective))
        ..type(SomeOtherType)
  static module() => _module;
}

This way not only can a directive implement different types, but it can also publish its own types.

@mhevery mhevery added this to the v1.0 milestone Feb 27, 2014
@vicb vicb self-assigned this Mar 3, 2014
@vicb vicb modified the milestones: v0.9.9, v1.0 Mar 3, 2014
@vicb vicb removed their assignment Mar 3, 2014
@jbdeboer jbdeboer modified the milestones: v1.0, v0.9.9 Mar 5, 2014
@mhevery
Copy link
Contributor Author

mhevery commented Mar 21, 2014

@kasperl can you add your thoughts here?

@mvuksano
Copy link
Contributor

PR #779 Has an implementation which allows specifying a module. The PR supports two syntaxes at the moment.

If we define types as:

class FooModule extends Module {
  FooModule() {
    type(FooComponent);
    install(new CustomModule());
  }
}

class CustomModule extends Module {
  CustomModule() {
    type(SomeType);
    factory(SomeType2, (_) => new SomeType2());
    type(SomeType3);
  }
}

class SomeType {
  int i = 1;
}

class SomeType2 {
  int i = 2;
}

class SomeType3 {
  SomeType someType;
  SomeType2 someType2;

  SomeType3(this.someType, this.someType2);

  int get i {
    return someType.i + someType2.i;
  }
}

Then we can define NgComponent as:

@NgComponent(selector: 'foo',
    publishAs: 'ctrl',
    templateUrl: 'packages/demo/component/foo.html',
    module: FooComponent.module)
class FooComponent {
  final SomeType someType;
  final SomeType2 someType2;
  final SomeType3 someType3;

  static Module module() => new CustomModule();

  FooComponent(this.someType, this.someType2, this.someType3) {
    print(someType.i);
    print(someType2.i);
    print(someType3.i);
  }
}

or

@NgComponent(selector: 'foo',
    publishAs: 'ctrl',
    templateUrl: 'packages/demo/component/foo.html',
    module: CustomModule)
class FooComponent {
  final SomeType someType;
  final SomeType2 someType2;
  final SomeType3 someType3;

  static Module module() => new CustomModule();

  FooComponent(this.someType, this.someType2, this.someType3) {
    print(someType.i);
    print(someType2.i);
    print(someType3.i);
  }
}

When using static function we don't have to use reflection to instantiate types. When using just type the end user does not need to worry about specifying static function but there might be some performance penalties.

Thoughts? Any other suggestions? @mhevery @pavelgj @jbdeboer @kasperl + anyone else who wants to contribute their thoughts :)

@kasperl
Copy link
Contributor

kasperl commented Mar 24, 2014

What's the current thinking on this topic? Are people generally happy with using a static method (module) that can be closurized in a compile-time constant way?

Are you still looking for alternatives, @mhevery?

@mhevery
Copy link
Contributor Author

mhevery commented Apr 2, 2014

Closed by: 5ec7e83

@mhevery mhevery closed this as completed Apr 2, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

5 participants