Skip to content

Commit

Permalink
PropertiesChanged
Browse files Browse the repository at this point in the history
* Integrate serializeValue fromPropertyAccessors.
* createProperties now takes an object rather than an array.
* ready always calls `flushProperties`.
* flushProperties only does work if changes are pending.
  • Loading branch information
Steven Orvell committed Nov 10, 2017
1 parent 1b50194 commit 05cb5d2
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions lib/mixins/properties-changed.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@

/**
* Creates property accessors for the given property names.
* @param {Array} props Array of names of accessors.
* @param {object} props Object whose keys are names of accessors.
*/
static createProperties(props) {
for (let i = 0; i < props.length; i++) {
this.prototype._createPropertyAccessor(props[i]);
for (let prop in props) {
this.prototype._createPropertyAccessor(prop);
}
}

Expand Down Expand Up @@ -136,6 +136,7 @@
*/
ready() {
this.__dataReady = true;
this._flushProperties();
}

/**
Expand Down Expand Up @@ -237,19 +238,12 @@
_invalidateProperties() {
if (!this.__dataInvalid && this.__dataReady) {
this.__dataInvalid = true;
microtask.run(() => this._validateProperties());
}
}

/**
* Changes are processed at microtask timing, but this method can be
* calleed to force any pending changes to be immediately processed.
* @protected
*/
_validateProperties() {
if (this.__dataInvalid) {
this.__dataInvalid = false;
this._flushProperties();
microtask.run(() => {
if (this.__dataInvalid) {
this.__dataInvalid = false;
this._flushProperties();
}
});
}
}

Expand Down Expand Up @@ -284,9 +278,11 @@
* @protected
*/
_flushProperties() {
let changedProps = this.__dataPending;
this.__dataPending = null;
this._propertiesChanged(this.__data, changedProps, this.__dataOld);
if (this.__dataPending && this.__dataOld) {
let changedProps = this.__dataPending;
this.__dataPending = null;
this._propertiesChanged(this.__data, changedProps, this.__dataOld);
}
}

/**
Expand Down Expand Up @@ -384,17 +380,30 @@
/**
* Sets a typed value to an HTML attribute on a node.
*
* If the value is `undefined`, the attribute will be removed.
* This method calls the `_serializeValue` method to convert the typed
* value to a string. If the `_serializeValue` method returns `undefined`,
* the attribute will be removed (this is the default for boolean
* type `false`).
*
* @param {Element} node Element to set attribute to.
* @param {*} value Value to serialize.
* @param {string} attribute Attribute name to serialize to.
*/
_valueToNodeAttribute(node, value, attribute) {
if (!value && value !== '' && value !== 0) {
const str = this._serializeValue(value);
if (str === undefined) {
node.removeAttribute(attribute);
} else {
node.setAttribute(attribute, value === true ? '' : value);
node.setAttribute(attribute, str);
}
}

_serializeValue(value) {
switch (typeof value) {
case 'boolean':
return value ? '' : undefined;
default:
return value != null ? value.toString() : undefined;
}
}

Expand Down Expand Up @@ -434,7 +443,7 @@
* @protected
*/
_attributeForProperty(property) {
return property;
return property.toLowerCase();
}

/**
Expand Down

0 comments on commit 05cb5d2

Please sign in to comment.