Skip to content

Commit

Permalink
Deserialize dash-case to camelCase
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Mar 5, 2015
1 parent 3d071ec commit 618756b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/features/micro/attributes.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="../../lib/annotations/annotations.html">

<script>

/**
Expand Down Expand Up @@ -52,7 +55,7 @@
* @class base feature: attributes
*/

using('Base', function(Base) {

This comment has been minimized.

Copy link
@pofigizm

pofigizm Mar 5, 2015

Contributor

Is it ok to include Annotation here?
attributes.html is base part of micro.

using(['Base', 'Annotations'], function(Base, Annotations) {

Base.addFeature({

Expand All @@ -78,40 +81,37 @@
},

_takeAttributesToModel: function(model) {
for (var name in this.properties) {
var type = this.getPropertyType(name);
if (type && this.hasAttribute(name)) {
this.setAttributeToProperty(model, name, type);
for (var propName in this.properties) {
var type = this.getPropertyType(propName);
var attrName = Annotations.camelToDashCase(propName);
if (type && this.hasAttribute(attrName)) {
var val = this.getAttribute(attrName);
model[propName] = this.deserialize(val, type);
}
}
},

setAttributeToProperty: function(model, name, type) {
if (this._serializing) {
// Don't deserialize back to property if currently reflecting
return;
}
type = type || this.getPropertyType(name);
if (type) {
model[name] = this.deserialize(name, this.getAttribute(name), type);
setAttributeToProperty: function(model, attrName) {
// Don't deserialize back to property if currently reflecting
if (!this._serializing) {
var propName = Annotations.dashToCamelCase(attrName);
var type = this.getPropertyType(propName);
if (type) {
var val = this.getAttribute(attrName);
model[propName] = this.deserialize(val, type);
}
}
},

deserialize: function(name, value, type) {
//
var hasAttribute = this.hasAttribute(name);
if (!hasAttribute && (type != Boolean) && (this[name] == null)) {
return;
}
//
deserialize: function(value, type) {
switch (type) {

case Number:
value = Number(value);
break;

case Boolean:
value = hasAttribute;
value = (value !== null);
break;

case Object:
Expand Down
9 changes: 9 additions & 0 deletions test/unit/attributes-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
date: {
type: Date,
value: function() { return new Date(0); }
},
dashCase: {
type: String,
value: 'none'
}
}
});
Expand Down Expand Up @@ -72,6 +76,11 @@
type: Date,
reflect: true,
value: function() { return new Date(0); }
},
dashCase: {
type: String,
reflect: true,
value: 'none'
}
}
});
Expand Down
6 changes: 6 additions & 0 deletions test/unit/attributes.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
string="The quick brown fox"
bool
date="Wed Mar 04 2015 10:46:05 GMT-0800 (PST)"
dash-case="The quick brown fox"
>
</x-basic>

Expand All @@ -39,6 +40,7 @@
string="The quick brown fox"
bool
date="Wed Mar 04 2015 10:46:05 GMT-0800 (PST)"
dash-case="The quick brown fox"
>
</x-basic>

Expand All @@ -60,6 +62,7 @@
assert.strictEqual(basicDefault.bool, true);
assert.strictEqual(basicDefault.negBool, false);
assert.strictEqual(basicDefault.date.getTime(), 0);
assert.strictEqual(basicDefault.dashCase, 'none');
});

test('basic deserialize attributes', function() {
Expand All @@ -70,6 +73,7 @@
assert.strictEqual(basicConfigured.bool, true);
assert.strictEqual(basicConfigured.negBool, false);
assert.strictEqual(basicConfigured.date.getTime(), configuredTime);
assert.strictEqual(basicConfigured.dashCase, configuredString);
});

test('reflected default values', function() {
Expand All @@ -80,6 +84,7 @@
assert.strictEqual(reflectDefault.bool, true);
assert.strictEqual(reflectDefault.negBool, false);
assert.strictEqual(reflectDefault.date.getTime(), 0);
assert.strictEqual(reflectDefault.dashCase, 'none');
});

test('reflected deserialize attributes', function() {
Expand All @@ -90,6 +95,7 @@
assert.strictEqual(reflectConfigured.bool, true);
assert.strictEqual(reflectConfigured.negBool, false);
assert.strictEqual(reflectConfigured.date.getTime(), configuredTime);
assert.strictEqual(reflectConfigured.dashCase, configuredString);
});

});
Expand Down

1 comment on commit 618756b

@pofigizm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also fix this bug.
pofigizm@7d23988
May be usefull.

Please sign in to comment.