Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "computed" and "observer" attribute to @property decorator #18

Merged
merged 14 commits into from
Dec 27, 2017
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Change Log

## [Unreleased]
- Added "observer" attribute to @property annotation
- Added "computed" attribute to @property annotation

## [0.1.1] - 2017-09-28
- Metadata Reflection polyfill is no longer a bower dependency.
Expand Down
12 changes: 6 additions & 6 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export interface PropertyOptions {
notify?: boolean;
reflectToAttribute?: boolean;
readOnly?: boolean;
computed?: string;
observer?: string;
}

/**
Expand All @@ -56,6 +58,8 @@ export function property(options?: PropertyOptions) {
const reflectToAttribute: boolean =
options && options.reflectToAttribute || false;
const readOnly: boolean = options && options.readOnly || false;
const computed: string = options && options.computed || '';
const observer: string = options && options.observer || '';

let type;
if (options && options.hasOwnProperty('type')) {
Expand All @@ -73,12 +77,8 @@ export function property(options?: PropertyOptions) {
if (!proto.constructor.hasOwnProperty('properties')) {
proto.constructor.properties = {};
}
proto.constructor.properties[propName] = {
type,
notify,
reflectToAttribute,
readOnly,
};
proto.constructor.properties[propName] =
{type, notify, reflectToAttribute, readOnly, computed, observer};
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/integration/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ suite('TypeScript Decorators', function() {
const propValue = testElement.readOnlyString;
chai.assert.equal(propValue, 'initial value');
});

test('computed property should return computed value', function() {
testElement.computedString = "new value";
const propValue = testElement.computedString;
chai.assert.equal(propValue, 'computed yahoo');
});

test('observer property function should be invoked', function() {
testElement.observedString = "new value";
const propValue = testElement.lastChange;
chai.assert.equal(propValue, 'new value');
});

});

Expand Down
16 changes: 16 additions & 0 deletions test/integration/elements/test-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,20 @@ class TestElement extends Polymer.Element {

@property({readOnly:true})
readOnlyString: string;

@property({computed:'computeString(reflectedString)'})
computedString: string;

@property({observer:'observeString'})
observedString: string;

// stand-in for set function dynamically created by Polymer on read only properties
_setReadOnlyString: (value: string) => void;

lastNumChange: number;

lastChange: string;

lastMultiChange: any[];

@query('#num')
Expand All @@ -60,4 +68,12 @@ class TestElement extends Polymer.Element {
private _numStringChanged(newNum: number, newString: string) {
this.lastMultiChange = [newNum, newString];
}

private computeString(s:string) {
return "computed " + s;
}

private observeString(s:string) {
this.lastChange = s;
}
}