Actually make computed readOnly:true. Add more tests.

This commit is contained in:
Kevin Schaaf
2015-06-24 15:56:50 -07:00
parent 259efb8aaf
commit 20b8fc22a9
4 changed files with 38 additions and 4 deletions

View File

@@ -174,8 +174,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
// ReadOnly properties have a private setter only
// TODO(kschaaf): Per current Bind factoring, we shouldn't
// be interrogating the prototype here
var info = model.getPropertyInfo && model.getPropertyInfo(property);
if (info && (info.readOnly || info.computed)) {
if (model.getPropertyInfo && model.getPropertyInfo(property).readOnly) {
model['_set' + this.upper(property)] = setter;
} else {
defun.set = setter;

View File

@@ -55,6 +55,8 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
this._addObserverEffect(p, prop.observer);
}
if (prop.computed) {
// Computed properties are implicitly readOnly
prop.readOnly = true;
this._addComputedEffect(p, prop.computed);
}
if (prop.notify) {
@@ -63,7 +65,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
if (prop.reflectToAttribute) {
this._addPropertyEffect(p, 'reflect');
}
if (prop.readOnly || prop.computed) {
if (prop.readOnly) {
// Ensure accessor is created
Polymer.Bind.ensurePropertyEffects(this, p);
}

View File

@@ -248,6 +248,10 @@
computedvalue="[[boundcomputedvalue]]"
computednotifyingvalue="[[boundcomputednotifyingvalue]]">
</x-basic>
<x-basic id="basic3"
notifyingvalue="{{computedValue}}"
computedvalue="{{value}}">
</x-basic>
</template>
<script>
Polymer({
@@ -259,6 +263,17 @@
'boundcomputednotifyingvalueChanged(boundcomputednotifyingvalue)',
'boundreadonlyvalueChanged(boundreadonlyvalue)'
],
properties: {
a: {
value: 10
},
b: {
value: 20
},
computedValue: {
computed: 'computeComputedValue(a, b)'
}
},
created: function() {
this.observerCounts = {
boundvalueChanged: 0,
@@ -268,6 +283,9 @@
boundreadonlyvalueChanged: 0
};
},
computeComputedValue: function(a, b) {
return a + b;
},
clearObserverCounts: function() {
for (var i in this.observerCounts) {
this.observerCounts[i] = 0;

View File

@@ -94,7 +94,7 @@ suite('single-element binding effects', function() {
assert.equal(el.$.boundChild.computedvalue, 45, 'Computed value not propagated to bound child');
});
test('computed value readOnly from outside', function() {
test('computed value readOnly from imperative set', function() {
el.value = 44;
// Should have no effect
el.computedvalue = 99;
@@ -303,6 +303,21 @@ suite('2-way binding effects between elements', function() {
assert.equal(el.observerCounts.boundcomputedvalueChanged, 0, 'observer for property bound to non-notifying computed property called and should not have been');
});
test('computed value readOnly from downward binding', function() {
el.$.basic3.value = 10;
assert.equal(el.$.basic3.computedvalue, 11);
// should have no effect
el.value = 99;
assert.equal(el.$.basic3.computedvalue, 11);
});
test('computed value readOnly from upward notification', function() {
assert.equal(el.computedValue, 30);
// should have no effect
el.$.basic3.notifyingvalue = 10;
assert.equal(el.computedValue, 30);
});
test('binding to notifying property', function() {
el.boundnotifyingvalue = 42;
assert.equal(el.$.basic1.notifyingvalue, 42, 'binding to child not updated');