Skip to content

Commit 366b82d

Browse files
author
Steve Orvell
authored
Merge pull request #3780 from Polymer/3779-kschaaf-prop-wins
Ensure properties override attributes at upgrade time. Fixes #3779.
2 parents 1916f87 + 95cd415 commit 366b82d

File tree

4 files changed

+90
-9
lines changed

4 files changed

+90
-9
lines changed

src/lib/template/dom-bind.html

+7-2
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@
7171
Polymer.RenderStatus.whenReady(function() {
7272
if (document.readyState == 'loading') {
7373
document.addEventListener('DOMContentLoaded', function() {
74-
self._markImportsReady();
74+
self._markImportsReady();
7575
});
76-
} else {
76+
} else {
7777
self._markImportsReady();
7878
}
7979
});
@@ -123,6 +123,11 @@
123123
}
124124
},
125125

126+
_configureInstanceProperties: function() {
127+
// We use the _prepConfigure code below to read instance values before
128+
// creating instance accessors, rather than the standard method here
129+
},
130+
126131
_prepConfigure: function() {
127132
var config = {};
128133
for (var prop in this._propertyEffects) {

src/standard/configure.html

+14-7
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@
9090
// e.g. hand template content stored in notes to children as part of
9191
// configure flow so templates have their content at ready time
9292
this._configureAnnotationReferences();
93+
// configure instance properties that may have been bound prior to upgrade
94+
this._configureInstanceProperties();
9395
// save copy of configuration that came from above
9496
this._aboveConfig = this.mixin({}, this._config);
9597
// get individual default values from property configs
@@ -113,18 +115,23 @@
113115
}
114116
},
115117

116-
_configureProperties: function(properties, config) {
117-
for (var i in properties) {
118-
var c = properties[i];
118+
_configureInstanceProperties: function() {
119+
for (var i in this._propertyEffects) {
119120
// Allow properties set before upgrade on the instance
120121
// to override default values. This allows late upgrade + an early set
121122
// to not b0rk accessors on the prototype.
122123
// Perf testing has shown `hasOwnProperty` to be ok here.
123-
if (!usePolyfillProto && this.hasOwnProperty(i) &&
124-
this._propertyEffects && this._propertyEffects[i]) {
125-
config[i] = this[i];
124+
if (!usePolyfillProto && this.hasOwnProperty(i)) {
125+
this._configValue(i, this[i]);
126126
delete this[i];
127-
} else if (c.value !== undefined) {
127+
}
128+
}
129+
},
130+
131+
_configureProperties: function(properties, config) {
132+
for (var i in properties) {
133+
var c = properties[i];
134+
if (c.value !== undefined) {
128135
var value = c.value;
129136
if (typeof value == 'function') {
130137
// pass existing config values (this._config) to value function

test/unit/configure-elements.html

+50
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,53 @@
234234
});
235235
</script>
236236
</dom-module>
237+
238+
<script>
239+
window.XConfigLazy = {
240+
is: 'x-config-lazy',
241+
properties: {
242+
noEffectProp: Number,
243+
defaultUsesNoEffectProp: {
244+
value: function() {
245+
return this.noEffectProp * 2;
246+
}
247+
},
248+
prop: {
249+
observer: 'propChanged'
250+
},
251+
readOnlyProp: {
252+
readOnly: true,
253+
value: 'readOnly'
254+
},
255+
hadAttrProp: {
256+
value: 'hadAttrProp',
257+
observer: 'hadAttrPropChanged'
258+
}
259+
},
260+
created: function() {
261+
this.noEffectProp = 1;
262+
this.propChanged = sinon.spy();
263+
this.hadAttrPropChanged = sinon.spy();
264+
}
265+
};
266+
</script>
267+
268+
<dom-module id="x-config-lazy-host">
269+
<template>
270+
<x-config-lazy id="lazy" prop="{{foo}}" read-only-prop="{{foo}}" had-attr-prop="attrValue"></x-config-lazy>
271+
</template>
272+
<script>
273+
Polymer({
274+
is: 'x-config-lazy-host',
275+
properties: {
276+
foo: {
277+
value: 'foo',
278+
observer: 'fooChanged'
279+
}
280+
},
281+
fooChanged: function(foo) {
282+
this.$.lazy.hadAttrProp = foo;
283+
}
284+
})
285+
</script>
286+
</dom-module>

test/unit/configure.html

+19
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,25 @@
186186
assert.equal(x.b, 2);
187187
document.body.removeChild(x);
188188
});
189+
190+
test('lazy upgrade binding use cases', function() {
191+
// don't test if __proto__ is polyfilled (IE10); cannot be fixed in this case.
192+
if (Polymer.Settings.usePolyfillProto) {
193+
return;
194+
}
195+
var el = document.createElement('x-config-lazy-host');
196+
document.body.appendChild(el);
197+
Polymer(window.XConfigLazy);
198+
assert.equal(el.$.lazy.noEffectProp, 1);
199+
assert.equal(el.$.lazy.defaultUsesNoEffectProp, 2);
200+
assert.equal(el.$.lazy.prop, 'foo');
201+
assert.isTrue(el.$.lazy.propChanged.calledOnce);
202+
assert.equal(el.$.lazy.readOnlyProp, 'readOnly');
203+
assert.equal(el.$.lazy.hadAttrProp, 'foo');
204+
assert.isTrue(el.$.lazy.hadAttrPropChanged.calledOnce);
205+
document.body.removeChild(el);
206+
});
207+
189208
});
190209

191210
</script>

0 commit comments

Comments
 (0)