Skip to content

Commit

Permalink
Coerce undefined to default when binding. Fixes #1742.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Jun 24, 2015
1 parent f62a80d commit bc71099
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
22 changes: 15 additions & 7 deletions src/standard/configure.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,26 @@

_configureProperties: function(properties, config) {
for (var i in properties) {
var c = properties[i];
if (c.value !== undefined) {
var value = c.value;
if (typeof value == 'function') {
// pass existing config values (this._config) to value function
value = value.call(this, this._config);
}
var value = this._defaultValueFromInfo(properties[i]);
if (value !== undefined) {
config[i] = value;
}
}
},

_defaultValueFromInfo: function(info) {
var value = info && info.value;
if (typeof value == 'function') {
// pass existing config values (this._config) to value function
value = value.call(this, this._config);
}
return value;
},

_defaultValueForProp: function(prop) {
return this._defaultValueFromInfo(this.getPropertyInfo(prop));
},

_mixinConfigure: function(a, b) {
for (var prop in b) {
if (!this.getPropertyInfo(prop).readOnly) {
Expand Down
4 changes: 4 additions & 0 deletions src/standard/effectBuilder.html
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@
(node.localName == 'input' && property == 'value')) {
value = value == undefined ? '' : value;
}
// coerce undefined back to default
if (value === undefined && node._defaultValueForProp) {
value = node._defaultValueForProp(property);
}
// TODO(kschaaf): Ideally we'd use `fromAbove: true`, but this
// breaks read-only properties
// this._setProperty(property, value, true, node);
Expand Down
17 changes: 10 additions & 7 deletions test/unit/notify-path-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
notifyingValue: {
type: Number,
notify: true
},
valueWithDefault: {
type: Number,
value: 99
}
}
});
Expand Down Expand Up @@ -50,7 +54,7 @@
},
objValueChanged: function(value) {
this.observerCounts.objValueChanged++;
assert.equal(this.obj.value, value);
assert.equal(this.obj && this.obj.value, value);
},
});
</script>
Expand Down Expand Up @@ -95,15 +99,15 @@
},
objValueChanged: function(value) {
this.observerCounts.objValueChanged++;
assert.equal(this.obj.value, value);
assert.equal(this.obj && this.obj.value, value);
},
});
</script>
</dom-module>

<dom-module id="x-stuff">
<template>
<x-basic id="basic" notifying-value="{{nested.obj.value}}" attrvalue$="{{nested.obj.value}}"></x-basic>
<x-basic id="basic" notifying-value="{{nested.obj.value}}" value-with-default="{{nested.obj.value}}" attrvalue$="{{nested.obj.value}}"></x-basic>
<x-compose id="compose" obj="{{nested.obj}}"></x-compose>
<x-forward id="forward" obj="{{nested.obj}}"></x-forward>
<div id="boundChild" computed-from-paths="{{computeFromPaths(a, nested.b, nested.obj.c)}}" array-length="{{data.length}}"></div>
Expand Down Expand Up @@ -157,11 +161,11 @@
},
nestedObjChanged: function(value) {
this.observerCounts.nestedObjChanged++;
assert.equal(this.nested.obj, value);
assert.equal(this.nested && this.nested.obj, value);
},
nestedObjSubpathChanged: function(change) {
this.observerCounts.nestedObjSubpathChanged++;
assert.equal(change.base, this.nested.obj);
assert.equal(change.base, this.nested && this.nested.obj);
if (this.expectedNestedObjSubpath) {
assert.equal(change.path, this.expectedNestedObjSubpath);
assert.equal(change.value, this.expectedNestedObjValue);
Expand All @@ -175,12 +179,11 @@
this.observerCounts.multipleChanged++;
assert.equal(a, 'a');
assert.equal(b, 'b');
assert.equal(nestedObjChange.base, this.nested.obj);
assert.equal(nestedObjChange.base, this.nested && this.nested.obj);
if (this.expectedNestedObjSubpath) {
assert.equal(nestedObjChange.path, this.expectedNestedObjSubpath);
assert.equal(nestedObjChange.value, this.expectedNestedObjValue);
}
assert.equal(nestedObjChange.base, this.nested.obj);
},
computeFromPaths: function(a, b, c) {
assert.equal(a, this.a, 'computeFromNested `a` arg incorrect');
Expand Down
13 changes: 13 additions & 0 deletions test/unit/notify-path.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@
assert.equal(el.$.forward.$.compose.$.basic2.getAttribute('attrvalue'), '42');
});

test('reset to default on null', function() {
// Setup
var nested = {
obj: {
value: 42
}
};
el.nested = nested;
assert.equal(el.$.basic.valueWithDefault, 42);
el.nested = null;
assert.equal(el.$.basic.valueWithDefault, 99);
});

test('notification from basic element property change', function() {
// Setup
var nested = {
Expand Down

0 comments on commit bc71099

Please sign in to comment.