Skip to content

Commit a121840

Browse files
committed
Make computed properties implicitly readOnly. Fixes #1925.
1 parent f62a80d commit a121840

File tree

5 files changed

+15
-4
lines changed

5 files changed

+15
-4
lines changed

PRIMER.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,8 @@ Polymer supports virtual properties whose values are calculated from other prope
15391539

15401540
*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.*
15411541

1542+
Computed properties are implicitly `readOnly`, and cannot be manually set.
1543+
15421544
```html
15431545
<dom-module id="x-custom">
15441546
<template>
@@ -1865,7 +1867,7 @@ Example:
18651867
<my-toolbar>My awesome app</my-toolbar>
18661868
<button on-tap="changeTheme">Change theme</button>
18671869
</template>
1868-
1870+
18691871
<script>
18701872
Polymer({
18711873
is: 'x-custom',

src/lib/bind/accessors.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@
174174
// ReadOnly properties have a private setter only
175175
// TODO(kschaaf): Per current Bind factoring, we shouldn't
176176
// be interrogating the prototype here
177-
if (model.getPropertyInfo && model.getPropertyInfo(property).readOnly) {
177+
var info = model.getPropertyInfo(property);
178+
if (model.getPropertyInfo && (info.readOnly || info.computed)) {
178179
model['_set' + this.upper(property)] = setter;
179180
} else {
180181
defun.set = setter;

src/lib/bind/effects.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
if (args) {
7878
var fn = this[effect.method];
7979
if (fn) {
80-
this[effect.property] = fn.apply(this, args);
80+
this._setProperty(effect.property, fn.apply(this, args));
8181
} else {
8282
this._warn(this._logf('_computeEffect', 'compute method `' +
8383
effect.method + '` not defined'));

src/standard/effectBuilder.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
if (prop.reflectToAttribute) {
6464
this._addPropertyEffect(p, 'reflect');
6565
}
66-
if (prop.readOnly) {
66+
if (prop.readOnly || prop.computed) {
6767
// Ensure accessor is created
6868
Polymer.Bind.ensurePropertyEffects(this, p);
6969
}

test/unit/bind.html

+8
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@
9494
assert.equal(el.$.boundChild.computedvalue, 45, 'Computed value not propagated to bound child');
9595
});
9696

97+
test('computed value readOnly from outside', function() {
98+
el.value = 44;
99+
// Should have no effect
100+
el.computedvalue = 99;
101+
assert.equal(el.computedvalue, 45, 'Computed value not correct');
102+
assert.equal(el.$.boundChild.computedvalue, 45, 'Computed value not propagated to bound child');
103+
});
104+
97105
test('computed values to same method updates', function() {
98106
el.value = 44;
99107
el.valuetwo = 144;

0 commit comments

Comments
 (0)