2016-12-14 8 views
0
class Test { 
    @First() 
    @Second() 
    public someAttribute; 
} 

var t = new Test(); 
var decorators = t.getListOfAttributeDecorators("someAttribute"); 
console.log(decorators); // [First, Second] 

"getListOfAttributeDecorators"関数を実装したいと思いますが、その方法はわかりません。または、属性デコレータのリストを取得する他の方法はありますか?TypeScriptの属性デコレータのリストを取得します

答えて

1

あなたはその情報、のようなものを保持するデータ構造を維持する必要があります:あなたはreflect-metadataを使用してカスタムのデコレータについてのデータを取得することができます

const REGISTRY = new Map<string, Map<string, string[]>>(); 
function register(cls: string, property: string, decorator: string) { 
    let map: Map<string, string[]>; 

    if (REGISTRY.has(cls)) { 
     map = REGISTRY.get(cls); 
    } else { 
     map = new Map<string, string[]>(); 
     REGISTRY.set(cls, map); 
    } 

    let list: string[]; 
    if (map.has(property)) { 
     list = map.get(property); 
    } else { 
     list = []; 
     map.set(property, list); 
    } 

    if (list.indexOf(decorator) < 0) { 
     list.push(decorator); 
    } 
} 

function First() { 
    return function (cls: any, property: string) { 
     register(cls.constructor.name, property, "First"); 
    } 
} 

function Second() { 
    return function (cls: any, property: string) { 
     register(cls.constructor.name, property, "Second"); 
    } 
} 

class Test { 
    @First() 
    @Second() 
    public someAttribute; 

    public getListOfAttributeDecorators(property: string): string[] { 
     const name = this.constructor.name; 
     return !REGISTRY.has(name) ? [] : REGISTRY.get(name).get(property); 
    } 
} 

let t = new Test(); 
let names = t.getListOfAttributeDecorators("someAttribute"); 
console.log(names); // ["Second", "First"] 

code in playground

0

を。デコレータの実装内でプロパティのメタデータを定義することによって可能です - see on codesandbox。あなたがサードパーティのライブラリは、多くの場合、異なるmetadata key

// be sure to import reflect-metadata 
// without importing reflect-metadata Reflect.defineMetadata and other will not be defined. 
import "reflect-metadata"; 

function First(target: Object, propertyKey: string | symbol) { 
    // define metadata with value "First" 
    Reflect.defineMetadata("custom:anotations:first", "First", target, propertyKey); 
} 

function Second(target: Object, propertyKey: string | symbol) { 
    // define metadata with value { second: 2 } 
    // be sure that metadata key is different from First 
    Reflect.defineMetadata("custom:anotations:second", { second: 2 }, target, propertyKey); 
} 

class Test { 
    @First 
    @Second 
    someAttribute: string; 
} 

// get decorators 

function getDecorators(target: any, propertyName: string | symbol): string[] { 
    // get info about keys that used in current property 
    const keys: any[] = Reflect.getMetadataKeys(target, propertyName); 
    const decorators = keys 
    // filter your custom decorators 
    .filter(key => key.toString().startsWith("custom:anotations")) 
    .reduce((values, key) => { 
     // get metadata value. 
     const currValues = Reflect.getMetadata(key, target, propertyName); 
     return values.concat(currValues); 
    }, []); 

    return decorators; 
} 

// test 

var t = new Test(); 
var decorators = getDecorators(t, "someAttribute"); // output is [{ second: 2}, "First"] 
console.log(decorators); 

で、このようなアプローチを使用するだけで、あなたのカスタムデコレータでそれを行うことができますが、メタデータを操作できるようにするために、あなたのtsconfig.json"emitDecoratorMetadata": trueを追加することを忘れないでください。

ボーナス:class decoratorsサポートと実装 - see on codesandox

P.S.それは古い質問ですが、私の答えが誰かを助けることを願っています。

関連する問題