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 all 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
5 changes: 3 additions & 2 deletions src/lib/bind/accessors.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
// TODO(kschaaf): downward bindings (e.g. _applyEffectValue) should also
// use non-notifying setters but right now that would require looking
// up readOnly property config in the hot-path
_setProperty: function(property, value, quiet, node) {
__setProperty: function(property, value, quiet, node) {
node = node || this;
var effects = node._propertyEffects && node._propertyEffects[property];
if (effects) {
Expand Down 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));
} else {
this._warn(this._logf('_computeEffect', 'compute method `' +
effect.method + '` not defined'));
Expand Down
10 changes: 5 additions & 5 deletions src/lib/template/dom-repeat.html
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@
if (!row) {
this.rows.push(row = this._insertRow(i, null, item));
}
row._setProperty(this.as, item, true);
row._setProperty('__key__', key, true);
row._setProperty(this.indexAs, i, true);
row.__setProperty(this.as, item, true);
row.__setProperty('__key__', key, true);
row.__setProperty(this.indexAs, i, true);
}
// Remove extra
for (; i<this.rows.length; i++) {
Expand Down Expand Up @@ -569,7 +569,7 @@
_forwardParentProp: function(prop, value) {
if (this.rows) {
this.rows.forEach(function(row) {
row._setProperty(prop, value, true);
row.__setProperty(prop, value, true);
}, this);
}
},
Expand Down Expand Up @@ -598,7 +598,7 @@
path = this.as + '.' + path.substring(dot+1);
row.notifyPath(path, value, true);
} else {
row._setProperty(this.as, value, true);
row.__setProperty(this.as, value, true);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/standard/configure.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
// Call _propertySet for any properties with accessors, which will
// initialize read-only properties also; set quietly if value was
// configured from above, as opposed to default
this._setProperty(n, config[n], n in aboveConfig);
this.__setProperty(n, config[n], n in aboveConfig);
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -265,7 +265,7 @@
}
// TODO(kschaaf): Ideally we'd use `fromAbove: true`, but this
// breaks read-only properties
// this._setProperty(property, value, true, node);
// this.__setProperty(property, value, true, node);
return node[property] = value;
}
},
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