Repository navigation
Properties docs - #380
Properties docs#380 5 commits merged into
Conversation
|
@kevinpschaaf @keanulee @sorvell Updated properties docs are staged here: https://20181218t010822-dot-polymer-lit-element.appspot.com/guide/properties Could you (or anyone) please comment on a few questions?
|
| * To **reflect** an attribute (set an attribute from a property), the property value must be converted to a string. | ||
|
|
||
| By default, `type`, `fromAttribute` and `toAttribute` are the `String` constructor. | ||
| LitElement defines a **converter** for every declared property. A converter is an object that contains two functions: |
There was a problem hiding this comment.
Maybe "LitElement accepts a converter for every declared property"?
Also, a converter can be an object with two functions, or just a function (with fromAttribute semantics).
| | `String` | Attribute value | Property value | | ||
| | `Number` | Number(attributeValue) | Property value | | ||
| | `Boolean` | `true` if non-`null`, `false` otherwise | `''` if truthy, `null` otherwise | | ||
| | `Array` | `JSON.parse(attributeValue)` | `null` if the property value is `null`; `JSON.stringify(propertyValue)` otherwise | |
There was a problem hiding this comment.
Actually, for objects and arrays toAttribute returns "null (attribute is removed) if the property value is null or undefined".
There was a problem hiding this comment.
Could you expand on that point? I somehow thought the property value being undefined wouldn't affect the attribute. Will try to find where I got that impression (could have been code comments or comments on another issue)
There was a problem hiding this comment.
Er never mind that, i guess it would be bad to JSON.stringify(undefined)...
| ``` | ||
|
|
||
| ### Configure reflection to attributes | ||
| Call `requestUpdate('propertyName', oldValue)` when implementing a custom property setter to ensure that changes to the property will trigger updates when required. Pass the name and old value of the property to `requestUpdate` so that any property options can be correctly applied. |
There was a problem hiding this comment.
So with the recent change, manually call requestUpdate is only required in the noAccessor case when we don't generate an accessor for them (should be rare), so I think this can move down to that section. It's obviously only required if they want the property to trigger an update.
Unless noAccessor is true, If a user writes an accessor with the same name as a declared property, a generated accessor is still created that calls requestUpdate, but it will "super" to the user accessor... similar to this:
// The original user accessor is saved off (`userAccessor`, with `get` and
// `set` functions), and then the user accessor is overwritten with the generated
// accessor, which "supers" to the saved accessor; looks roughly like this:
set foo(value) {
const oldValue = this.foo;
userAccessor.set.call(this, value);
this.requestUpdate('foo' oldValue);
}
get foo() {
return userAccessor.get.call(this);
}I think 👆is probably too much implementation detail to put in code, not sure. But thought it would be useful to understand what we do under the covers to make sure it aligns with the docs.
fixes #135
fixes #372
fixes #354
fixes #375
fixes #318
https://20181218t132131-dot-polymer-lit-element.appspot.com/guide/properties
^^^updated with feedback