Cannot set filter in User Manager

Issue #893 resolved
Brian Lewis repo owner created an issue

When selecting a filter type, the list of options is not populated in the ‘filter value’ md-select

Comments (3)

  1. Brian Lewis reporter

    Also, it does not save the filter when it is set. This is becuase of issues converting to JSON when the value is a getter/setter.

    see

    https://stackoverflow.com/questions/42107492/json-stringify-es6-class-property-with-getter-setter

    for a discussion. If we really want to use getter/setters for properties, we may need to provide a custom toJSON implementation.

    Note that angularjs does quite a bit more than call `JSON.stringify() when sending an object to the server:

    see angularjs.js toJson

  2. Brian Lewis reporter

    In the end, giving up on a general solution to this problem of getter/setters: the closest is to add this:

            public toJSON = () => {
                // start with the plain object
                const jsonObj = _.clone((<any>this).plain ? (<any>this).plain() : this);
                // add all properties
                const proto = Object.getPrototypeOf(this);
                for (const key of Object.getOwnPropertyNames(proto)) {
                    const desc = Object.getOwnPropertyDescriptor(proto, key);
                    const hasGetter = desc && typeof desc.get === 'function';
                    if (hasGetter) {
                        jsonObj[key] = this[key];
                    }
                }
    
                return jsonObj;
            }
    

    to Editable base class, but then any activity in _beforeSave is lost. 😞

    So solution is: whenever a class such as User has a property implemented as a getter/setter that needs to be persisted, set its value in _beforesave

  3. Log in to comment