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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move both items to the unreleased section and remove these alpha releases?

- 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 should return computed value', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a tiny typo here, should should

testElement.computedString = "new value";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess this should be setting reflectedString to yahoo? so the following computed result is computed yahoo? by the looks of the test element source.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

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
21 changes: 17 additions & 4 deletions test/integration/elements/test-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* rights grant found at http://polymer.github.io/PATENTS.txt
*/

/// <reference path="../bower_components/polymer-decorators/global.d.ts" />
/// <reference path="bower_components/polymer-decorators/global.d.ts" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your build fails because this path can't be found, probably just needs setting back to ../?


const {customElement, property, query, queryAll, observe} = Polymer.decorators;

Expand All @@ -18,9 +18,6 @@ class TestElement extends Polymer.Element {
@property({notify: true})
aNum: number = 42;

@property()
doesntNotify: boolean = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a failing test causing your build to fail which depends on this. maybe just restore this property


@property({notify: true})
aString: string = 'yes';

Expand All @@ -32,12 +29,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 +65,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;
}
}