Hi Elias, Hi Zlatko,

I have a first solution.


"use strict";

import {EventEmitter} from "angular2/core";
import {Output} from "angular2/core";

export function InAndOut(target:any, name:any) {


    ///// CREATE GETTER UND SETTER FOR PROPERTY

    var descriptor:PropertyDescriptor = Object.getOwnPropertyDescriptor(target, 
name);
    if (descriptor) {

        // f.i. handle an previous decorator for the property

        Object.defineProperty(target, name, {
            get: function ():any {
                return descriptor.get.call(this);
            },
            set: function (newValue:any) {
                descriptor.set.call(this, newValue);
                // emit
                this[name + "Change"].emit(newValue);
            },
            enumerable: true,
            configurable: true
        });

    } else {

        // store property
        var _val:any;

        // Create new property
        Object.defineProperty(target, name, {
            get: function ():any {
                return _val;
            },
            set: function (newValue:any) {
                _val = newValue;
                // emit
                this[name + "Change"].emit(newValue);
            },
            enumerable: true,
            configurable: true
        });

    }

    ///// CREATE EMITTER FOR PROPERTY

    // emitter
    var _emitter = new EventEmitter();

    // Create new emitter property
    Object.defineProperty(target, name + "Change", {
        get: function () {
            return _emitter;
        },
        set: function (newVal:any) {
            _emitter = newVal;
        },
        enumerable: true,
        configurable: true
    });

    // create @Output decorator for the emitter
    Reflect["decorate"]([
        Output()
    ], target, name + "Change");


}


With this decorator I can now write


@InAndOut seconds:number = 0;




The decorator function is a bit more complicated now. But now you can 
combine the decorator with other decorators, like:


@logProperty
@InAndOut
test:number = 111;



Still, there are some questions:

*1) I recognized that I get an error if I write Reflect.decorate(...) so I 
cheated with Reflect["decorate"](...). Do you have any idea what I am doing 
wrong?*

*2) I am wondering, why this decorator is not part of the angular 2, while 
it is so easy to write. Is there a good reason, why I should avoid using 
@InAndOut?*

Thanks for your suggestions guys. 

Regards,

Jan






Am Donnerstag, 5. Mai 2016 17:38:16 UTC+2 schrieb Sander Elias:
>
> Hi Jan,
>
> I think there is, you can import the @output decorator in your own 
> decorator, and set it up from there. Might hurt testability of the 
> decorator a bit, and it overrules the injector. However, I think this is 
> fair game in this case :)
>
> Regards
> Sander
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to