Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Fixes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
sorvell committed Apr 24, 2014
1 parent 75434ee commit 65924e7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
38 changes: 25 additions & 13 deletions src/declaration/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,39 @@
prototype._publishLC = this.lowerCaseMap(publish);
}
},
requireProperties: function(properties, prototype, base) {
// sync prototype to property descriptors;
// desriptor format contains default value and optionally a
// hint for reflecting the property to an attribute.
// e.g. {foo: 5, bar: {value: true, reflect: true}}
// reflect: {foo: true} is also supported
//
requireProperties: function(propertyDescriptors, prototype, base) {
// reflected properties
prototype.reflect = prototype.reflect || {};
// ensure a prototype value for each property
for (var n in properties) {
if (this.valueReflects(properties[n])) {
prototype.reflect[n] = true;
// and update the property's reflect to attribute status
for (var n in propertyDescriptors) {
var propertyDescriptor = propertyDescriptors[n];
var reflects = this.reflectHintForDescriptor(propertyDescriptor);
if (prototype.reflect[n] === undefined && reflects !== undefined) {
prototype.reflect[n] = reflects;
}
if (prototype[n] === undefined && base[n] === undefined) {
prototype[n] = this.valueForProperty(properties[n]);
if (prototype[n] === undefined) {
prototype[n] = this.valueForDescriptor(propertyDescriptor);
}
}
},
valueForProperty: function(propertyValue) {
return (typeof propertyValue === 'object' && propertyValue !== null) ?
(propertyValue.value !== undefined ? propertyValue.value : null) :
propertyValue;
valueForDescriptor: function(propertyDescriptor) {
var value = typeof propertyDescriptor === 'object' &&
propertyDescriptor ? propertyDescriptor.value : propertyDescriptor;
return value !== undefined ? value : null;
},
valueReflects: function(propertyValue) {
return (typeof propertyValue === 'object' && propertyValue !== null &&
propertyValue.reflect);
// returns the value of the descriptor's 'reflect' property or undefined
reflectHintForDescriptor: function(propertyDescriptor) {
if (typeof propertyDescriptor === 'object' &&
propertyDescriptor && propertyDescriptor.reflect !== undefined) {
return propertyDescriptor.reflect;
}
},
lowerCaseMap: function(properties) {
var map = {};
Expand Down
22 changes: 22 additions & 0 deletions test/html/prop-attr-reflection.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@
});
</script>
</polymer-element>

<x-zot></x-zot>
<polymer-element name="x-zot" extends="x-bar">
<script>
Polymer('x-zot', {
publish: {
zot: {value: 2, reflect: false},
str: 'str2'
}
});
</script>
</polymer-element>

<x-compose></x-compose>
<polymer-element name="x-compose">
Expand Down Expand Up @@ -109,6 +121,16 @@
//assert.isFalse(xbar.hasAttribute('obj'), 'property with default type of object does not serialize');
done();
});

var xzot = document.querySelector('x-zot');
assert.equal(xzot.str, 'str2');
xzot.str = 'hello';
assert.equal(xzot.getAttribute('str'), xzot.str);

assert.equal(xzot.zot, 2);
xzot.zot = 5;
assert.isFalse(xzot.hasAttribute('zot'), 'extendee reflect false not honored');

xbar.obj = 'hi';
Platform.flush();
// trigger a mutation to watch
Expand Down
12 changes: 12 additions & 0 deletions test/html/publish-attributes.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@
assert.deepEqual(
XBar.prototype.publish,
{Foo: null, baz: null, Bar: null});
assert.deepEqual(
XZot.prototype.publish,
{Foo: null, baz: null, Bar: null, zot: 3});
assert.deepEqual(
XSquid.prototype.publish,
{Foo: null, baz: 13, Bar: null, zot: 5, squid: 7});
assert.equal(XSquid.prototype.Foo, XSquid.prototype.publish.Foo);
assert.equal(XSquid.prototype.baz, XSquid.prototype.publish.baz);
assert.equal(XSquid.prototype.Bar, XSquid.prototype.publish.Bar);
assert.equal(XSquid.prototype.zot, XSquid.prototype.publish.zot);
assert.equal(XSquid.prototype.squid, XSquid.prototype.publish.squid);

done();
});
</script>
Expand Down

0 comments on commit 65924e7

Please sign in to comment.