1.0.2 • Published 9 years ago

decorator-metadata v1.0.2

Weekly downloads
4
License
MIT
Repository
github
Last release
9 years ago

Usage

Define your metadata functions like this:

import Metadata from 'decorator-metadata';

function myClassDecoration(metadataToSave) {
    return Metadata.DecorateClassWithParams(myDecoration, metadataToSave);
}

function myClassDecorationBool() {
    return Metadata.DecorateClass(myDecoration, ...arguments);
}

function myDecoration(metadataToSave) {
    return Metadata.DecorateWithParams(myDecoration, metadataToSave);
}

function myDecorationBool() {
    return Metadata.Decorate(myDecoration, ...arguments);
}

Now you can decorate as follows:

@myClassDecoration('this string will be stored along with the class')
@myClassDecorationBool // only the existence of this decorator will be saved 
class ClassWithMetadata {

    @myDecoration('this string will be stored along with the method')
    someMethod() {
        ...
    }
    
    @myDecorationBool
    otherMethod() {
        ...
    }
}

Retrieve the metadata:

let classesDecoratedWithMyClassDecoration = Metadata.GetAllClassesDecoratedWith(myClassDecoration);
let classesDecoratedWithMyClassDecorationBool = Metadata.GetAllClassesDecoratedWith(myClassDecorationBool);

Get specific instances of decoration and passed in parameters:

for (let decoratedClass of classesDecoratedWithMyClassDecoration) {
    let classMetadata = Metadata.Get(decoratedClass);
    
    let decorationInstances = classMetadata.getDecorations(myClassDecoration);
    let decorationInstance = decorationInstances[0];
    let argumentsPassed = classMetadata.getDecorationArguments(decoratedClass, decorationInstance);
    
    console.log(argumentsPassed);
    // prints: 
    // [0] => 'this string will be stored along with the class'
}