Skip to content

Commit

Permalink
don't serialize properties whose original value is object; slight sim…
Browse files Browse the repository at this point in the history
…plification
  • Loading branch information
sorvell committed Aug 20, 2013
1 parent 6958462 commit b4f9720
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
26 changes: 16 additions & 10 deletions src/instance/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,30 @@
deserializeValue: function(stringValue, defaultValue) {
return scope.deserializeValue(stringValue, defaultValue);
},
serializeValue: function(value) {
if (typeof value != 'object' && value !== undefined) {
serializeValue: function(value, inferredType) {
if (inferredType === 'boolean') {
return value ? '' : undefined;
} else if (inferredType !== 'object' && typeof value !== 'object' &&
value !== undefined) {
return value;
}
},
propertyToAttribute: function(name) {
if (Object.keys(this[PUBLISHED]).indexOf(name) >= 0) {
var serializedValue = this.serializeValue(this[name]);
var inferredType = typeof this.__proto__[name];
var serializedValue = this.serializeValue(this[name], inferredType);
// boolean properties must reflect as boolean attributes
if (typeof this.__proto__[name] === 'boolean') {
if (serializedValue) {
this.setAttribute(name, '');
} else {
this.removeAttribute(name);
}
} else if (serializedValue !== undefined) {
if (serializedValue !== undefined) {
this.setAttribute(name, serializedValue);
// TODO(sorvell): we should remove attr for all properties
// that have undefined serialization; however, we will need to
// refine the attr reflection system to achieve this; pica, for example,
// relies on having inferredType object properties not removed as
// attrs.
} else if (inferredType === 'boolean') {
this.removeAttribute(name);
}

}
}
};
Expand Down
17 changes: 13 additions & 4 deletions test/html/prop-attr-reflection.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
<x-foo></x-foo>
<polymer-element name="x-foo" attributes="foo baz">
<script>
Polymer('x-foo');
Polymer('x-foo', {
foo: '',
baz: ''
});
</script>
</polymer-element>

Expand Down Expand Up @@ -69,12 +72,12 @@
Platform.endOfMicrotask(function() {
assert.equal(xbar.foo, xbar.getAttribute('foo'), 'inherited published property is reflected');
assert.equal(String(xbar.zot), xbar.getAttribute('zot'), 'attribute reflects property as number');
assert.equal('', xbar.getAttribute('zim'), 'attribute reflects true valued boolean property as having attribute');
assert.equal(xbar.getAttribute('zim'), '', 'attribute reflects true valued boolean property as having attribute');
assert.equal(xbar.str, xbar.getAttribute('str'), 'attribute reflects property as published string');
assert.isFalse(xbar.hasAttribute('obj'), 'attribute does not reflect object property');
xbar.setAttribute('zim', 'false');
xbar.setAttribute('foo', 'foo!!');
xbar.setAttribute('zot', 54);
xbar.setAttribute('zim', 'false');
xbar.setAttribute('str', 'str!!');
xbar.setAttribute('obj', "{'hello': 'world'}");
assert.equal(xbar.foo, xbar.getAttribute('foo'), 'property reflects attribute as string');
Expand All @@ -86,7 +89,13 @@
Platform.flush();
Platform.endOfMicrotask(function() {
assert.isFalse(xbar.hasAttribute('zim'), 'attribute reflects false valued boolean property as NOT having attribute');
done();
var objAttr = xbar.getAttribute('obj');
xbar.obj = 'hi';
Platform.endOfMicrotask(function() {
assert.equal(xbar.getAttribute('obj'), objAttr, 'do not reflect property with default type of object');
//assert.isFalse(xbar.hasAttribute('obj'), 'property with default type of object does not serialize');
done();
});
});
});
});
Expand Down

0 comments on commit b4f9720

Please sign in to comment.