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

Make computed properties implicitly readOnly. Fixes #1925. #1958

Merged
merged 3 commits into from
Jun 24, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion PRIMER.md
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,8 @@ Polymer supports virtual properties whose values are calculated from other prope

*Note, computing functions will only be called once all dependent properties are defined (`!=undefined`). If one or more of the properties are optional, they would need default `value`'s defined in `properties` to ensure the property is computed.*

Computed properties are implicitly `readOnly`, and cannot be manually set.

```html
<dom-module id="x-custom">
<template>
Expand Down Expand Up @@ -1865,7 +1867,7 @@ Example:
<my-toolbar>My awesome app</my-toolbar>
<button on-tap="changeTheme">Change theme</button>
</template>

<script>
Polymer({
is: 'x-custom',
Expand Down
3 changes: 2 additions & 1 deletion src/lib/bind/accessors.html
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@
// ReadOnly properties have a private setter only
// TODO(kschaaf): Per current Bind factoring, we shouldn't
// be interrogating the prototype here
if (model.getPropertyInfo && model.getPropertyInfo(property).readOnly) {
var info = model.getPropertyInfo && model.getPropertyInfo(property);
if (info && (info.readOnly || info.computed)) {
model['_set' + this.upper(property)] = setter;
} else {
defun.set = setter;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/bind/effects.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
if (args) {
var fn = this[effect.method];
if (fn) {
this[effect.property] = fn.apply(this, args);
this._setProperty(effect.property, fn.apply(this, args));
Copy link
Contributor

Choose a reason for hiding this comment

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

Cannot make a readOnly property named property. Perhaps we should change the name of _setProperty?

} else {
this._warn(this._logf('_computeEffect', 'compute method `' +
effect.method + '` not defined'));
Expand Down
2 changes: 1 addition & 1 deletion src/standard/effectBuilder.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
if (prop.reflectToAttribute) {
this._addPropertyEffect(p, 'reflect');
}
if (prop.readOnly) {
if (prop.readOnly || prop.computed) {
// Ensure accessor is created
Polymer.Bind.ensurePropertyEffects(this, p);
}
Expand Down
8 changes: 8 additions & 0 deletions test/unit/bind.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@
assert.equal(el.$.boundChild.computedvalue, 45, 'Computed value not propagated to bound child');
});

test('computed value readOnly from outside', function() {
el.value = 44;
// Should have no effect
el.computedvalue = 99;
assert.equal(el.computedvalue, 45, 'Computed value not correct');
assert.equal(el.$.boundChild.computedvalue, 45, 'Computed value not propagated to bound child');
});

test('computed values to same method updates', function() {
el.value = 44;
el.valuetwo = 144;
Expand Down