Отражение Typescript не будет обновлять значение объекта - PullRequest
0 голосов
/ 21 мая 2019

У меня проблема с обновлением значения объекта в метаданных Reflect, когда я пытался с помощью decorator инициализировать декоратор свойства, я успешно инициализировал значение, но проблема в том, что оно не обновляет значение, когда init свойство в NgOnInit или любая другая функция вмой компонент, и это то, что я сделал в этом далеком

успешном значении инициализации в свойствах

// initialized property
@BtnProp(
    {key: 'btnPencarianBelanja', 
    icon: 'pi pi-search', 
    position: IBottonPosition.RIGHT, 
    class:'ui-button-secondary', 
    width: '35px', 
    height: '35px'})
  btnPencariabtnPencarianBelanja: IToolBotton;


  @BtnProps()
  toolBottons: IToolBotton[];

это моя функция декоратор

import "reflect-metadata";
import { IToolBotton } from './toolbotton.interface';

export const PROPERTY_METADATA_KEY = Symbol("BtnProp");

export function BtnProp(props: IToolBotton) {

    return (target: any, propertyKey: string | symbol) => {
        let value: IToolBotton;
        const update = Reflect.defineProperty(
            target,
            propertyKey,
            {
                configurable: true,
                enumerable: true,
                get: () => {
                    // Return the scoped value
                    return !value ? props : value;
                },
                set:(val: IToolBotton) => {
                    // Update the scoped value with val || props
                    // but the value wont updated
                    value = (val !== props? val : props);
                }
            },
        );
        if(!update) {
            console.log("Unable to update property");
        }

        const allMetadata = (
            Reflect.getMetadata(PROPERTY_METADATA_KEY, target)
            ||
            {}
        );
        console.log(allMetadata);
        // Ensure allMetadata has propertyKey
        allMetadata[propertyKey] = (
            allMetadata[propertyKey]
            ||
            {}
        );

        // Update the metadata with anything from updates
        // tslint:disable-next-line:forin
        for (const key of Reflect.ownKeys(props)) {
            allMetadata[propertyKey][key] = props[key];
        }


        Reflect.defineMetadata(
            PROPERTY_METADATA_KEY,
            allMetadata,
            target,
        );
    };

}

export function BtnProps(){
    return (target: any, propertyKey: string | symbol) => {
        let allMetadata: any = Reflect.getMetadata(PROPERTY_METADATA_KEY, target);
        let toolbottons: IToolBotton[] = [];
        for(const meta in allMetadata) {
            const botton:IToolBotton = (<IToolBotton>allMetadata[meta]);
            if(botton) {
                toolbottons.push(botton);
            } 
        }
        const update = Reflect.defineProperty(
            target,
            propertyKey,
            {
                configurable: true,
                enumerable: true,
                get: () => {
                    // Return the scoped value
                    return toolbottons;
                },
                set:(val: IToolBotton[]) => {
                    // Update the scoped value with val || props
                    toolbottons = val
                }
            },
        );
        if(!update) {
            console.log("Unable to update property");
        }
    }
}


попытался обновитьзначение в ngOnInit

async ngOnInit() {
....
// update value
    this.btnPencariabtnPencarianBelanja.width = '10px';

    console.log(this.toolBottons);// the result still same with the initialized attr property

}
...