0

I'm using reflect-metadata 0.1.2. I have a parent class as "MyCustom".

export class MyCustom {}

My "Home" component class extend this "MyCustom" class.

@Component({
  selector: 'home',
  templateUrl: './app/components/home/home.html',
  styleUrls: ['./app/components/home/home.css']
})
export class Home extends MyCustom {
}

My intention is to get metadata of all the classes that extend "MyCustom" class using following method call.

let annotations = Reflect.getMetadata('annotations', MyCustom);

The comments on the method Reflect.getMetadata() says,

Gets the metadata value for the provided metadata key on the target object or its prototype chain.

However I get nothing. If I add @Component to "MyCustom" class like below,

@Component({
  selector: 'hello'
})
export class MyCustom {}

I get one result that is the annotation on "MyCustom".

Why am I not getting annotations on subclasses ? Any help is highly appreciated.

Rajind Ruparathna
  • 2,215
  • 24
  • 55

1 Answers1

1

The metadata is saved for Home and not for MyCustom, and the inheritance has no effect here.

What you can probably do, is to have your own decorator which will add metadata for the parent class and then call the Component decorator with the needed values.
Something like:

function MyComponent(props: any) {
    return (ctor) => {
        Reflect.defineMetadata('annotations', ctor.prototype.name, ctor.prototype);
        return Component(props)(ctor);
    }
}

@MyComponent({
    selector: 'home',
    templateUrl: './app/components/home/home.html',
    styleUrls: ['./app/components/home/home.css']
})
class Home extends MyCustom {}

I haven't tested it, and haven't done anything similar before, but it should probably work.


Why don't you use a "registry"?
Have something like this:

const REGISTRY: { [name: string]: MyCustom } = {};

Then, for each such component register it:

class Home extends MyCustom {
    ...
}

REGISTRY["home"] = Home;

The registry and the components don't have to be in the same file, as long as you import it where needed.

Then from your main component it's easy to have all of those components.

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • Thanks for your response. I've tried to get it to work using this but no luck so far. Let me give the complete overview of what I am trying to do. My main application does not know at start about the sub components (basically tabs in view). I am trying annotate the subcomponents using a custom annotation and scan them from my main component and add them. – Rajind Ruparathna Oct 11 '16 at 06:35
  • Only way to to scan for annotations was Reflect.getMetadata() which requires target Object. That is why I was trying to use a super class as "MyCustom" and let my subcomponents extend that. ( At that point I was hoping that Reflect.getMetadata('annotations', MyCustom); will return annotations of subclasses as well ) – Rajind Ruparathna Oct 11 '16 at 06:45