Skip to content

Commit

Permalink
Move property<->attribute case mapping to PropertiesChanged.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Orvell committed Nov 29, 2017
1 parent 82cf96b commit 603123e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
48 changes: 43 additions & 5 deletions lib/mixins/properties-changed.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,39 @@
/** @const {!AsyncInterface} */
const microtask = Polymer.Async.microTask;

/**
* Returns the map matching attribute names to properties. This map is
* used in `propertyNameForAttribute` which locks property names and
* attribute names together using this data.
*
* @private
* @param {PropertiesChangedConstructor} constructor PropertiesChanged
* constructor
* @return {Object} Object mapping property names to attributes.
*/
function getAttributeToPropertyMap(constructor) {
return constructor.__attributeToPropertyMap;
}

/**
* Adds to the `attributeToPropertyMap` for the given class. A key for the
* attribute discovered via `attributeNameForProperty` points to the
* given property name. This map is used in `propertyNameForAttribute`.
*
* @private
* @param {PropertiesChangedConstructor} constructor PropertiesChanged
* constructor
* @param {string} property Property name.
*/
function addToAttributeToPropertyMap(constructor, property) {
if (!constructor.hasOwnProperty(JSCompiler_renameProperty(
'__attributeToPropertyMap', constructor))) {
constructor.__attributeToPropertyMap = {};
}
const attr = constructor.attributeNameForProperty(property);
constructor.__attributeToPropertyMap[attr] = property;
}

/**
* Element class mixin that provides basic meta-programming for creating one
* or more property accessors (getter/setter pair) that enqueue an async
Expand Down Expand Up @@ -70,25 +103,29 @@

/**
* Returns a property name that corresponds to the given attribute.
* By default, converts dash to camel case, e.g. `foo-bar` to `fooBar`.
* The attribute name is always lowercase and the property name is the
* cased version for which a property accessor was created. Override to
* customize this mapping.
* @param {string} attribute Attribute to convert
* @return {string} Property name corresponding to the given attribute.
*
* @protected
*/
static propertyNameForAttribute(attribute) {
return attribute;
static propertyNameForAttribute(attribute) {
const map = getAttributeToPropertyMap(this);
return map && map[attribute] || attribute;
}

/**
* Returns an attribute name that corresponds to the given property.
* By default, converts camel to dash case, e.g. `fooBar` to `foo-bar`.
* The attribute name is the lowercased property name. Override to
* customize this mapping.
* @param {string} property Property to convert
* @return {string} Attribute name corresponding to the given property.
*
* @protected
*/
static attributeNameForProperty(property) {
static attributeNameForProperty(property) {
return property.toLowerCase();
}

Expand Down Expand Up @@ -122,6 +159,7 @@
}
if (!this.__dataHasAccessor[property]) {
this.__dataHasAccessor[property] = true;
addToAttributeToPropertyMap(this.constructor, property);
this._definePropertyAccessor(property, readOnly);
}
}
Expand Down
36 changes: 0 additions & 36 deletions lib/mixins/properties-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,6 @@
return constructor.__ownProperties;
}

/**
* Creates a map matching attribute names to properties by using
* `attributeNameForProperty` and the names in `properties`. This map is
* used in `propertyNameForAttribute` which locks property names and
* attribute names together using this data.
*
* @private
* @param {PropertiesClassConstructor} constructor PropertiesClass constructor
* @return {Object} Object mapping property names to attributes.
*/
function propertyNameForAttributeMap(constructor) {
if (!constructor.hasOwnProperty(JSCompiler_renameProperty(
'__propertyNameForAttributeMap', constructor))) {
const map = constructor.__propertyNameForAttributeMap = {};
const props = constructor._properties;
for (let prop in props) {
const attr = constructor.attributeNameForProperty(prop);
map[attr] = prop;
}
}
return constructor.__propertyNameForAttributeMap;
}

/**
* @polymer
* @mixinClass
Expand Down Expand Up @@ -187,19 +164,6 @@
return this.__properties;
}

/**
* Overrides PropertiesChanged implementation to discover the property
* associated with an attribute by revers mapping the property names in
* the `properties` object to the attribute name generated via
* `attributeNameForProperty`.
*
* @param {string} name Name of property
* @return {string} Name of attribute
*/
static propertyNameForAttribute(name) {
return propertyNameForAttributeMap(this)[name];
}

/**
* Overrides PropertiesChanged method to return type specified in the
* static `properties` object for the given property.
Expand Down

0 comments on commit 603123e

Please sign in to comment.