Skip to content

Commit 43104c9

Browse files
43081j43081j
authored and
43081j
committed
define computed getters
1 parent 5ca58d9 commit 43104c9

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/decorators.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -106,24 +106,26 @@ export function observe(targets: string|string[]) {
106106
}
107107

108108
/**
109-
* A TypeScript accessor decorator factory that causes the decorated accessor to
110-
* be called when a property changes. `targets` is either a single property
111-
* name, or a list of property names.
109+
* A TypeScript accessor decorator factory that causes the decorated getter to
110+
* be called when a set of dependencies change. The arguments of this decorator
111+
* should be paths of the data dependencies as described
112+
* [here](https://www.polymer-project.org/2.0/docs/devguide/observers#define-a-computed-property)
113+
* The decorated getter should not have an associated setter.
112114
*
113115
* This function must be invoked to return a decorator.
114116
*/
115117
export function computed<T = any>(...targets: (keyof T)[]) {
116118
return (proto: any, propName: string, descriptor: PropertyDescriptor): void => {
117-
const targetString = targets.join(',');
118-
const propNameCased = propName[0].toUpperCase() + propName.slice(1);
119-
const fnName = `__compute${propNameCased}`;
119+
const fnName = `__compute${propName}`;
120120

121-
proto.constructor.prototype[fnName] = descriptor.get;
121+
Object.defineProperty(proto, fnName, {
122+
get: descriptor.get
123+
});
122124

123125
descriptor.get = undefined;
124126

125127
createProperty(proto, propName, {
126-
computed: `${fnName}(${targetString})`
128+
computed: `${fnName}(${targets.join(',')})`
127129
});
128130
};
129131
}

0 commit comments

Comments
 (0)