Skip to content

Properties docs - #380

Merged
5 commits merged into
masterfrom
properties-docs
Dec 19, 2018
Merged

5 commits merged into
masterfrom
properties-docs

Conversation

@ghost

@ghost ghost commented Dec 18, 2018 •

Copy link
Copy Markdown

@ghost
ghost requested a review from sorvell as a code owner December 18, 2018 09:05
@ghost

ghost commented Dec 18, 2018 •

Copy link
Copy Markdown
Author

@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?

  • When should the user set noAccessor: true? (if they don't want updates/want to handle themselves)
  • If I specify noAccessor & don't create my own accessors, what should happen?
  • If I don't specify noAccessor & do create my own accesors, what should happen? (generates accessors, calls them & calls your own) Would there be a situation where you want to let LitElement create the generated ones & also create your own? When/why? (yes - to handle updates/property options etc)
  • Should the user care/be able to find out what LitElement has called their generated property accessors? If so how can they do that? (don't think they should care)

Comment thread docs/_guide/properties.md Outdated
* 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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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).

Comment thread docs/_guide/properties.md Outdated
| `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 |

@kevinpschaaf kevinpschaaf Dec 18, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Actually, for objects and arrays toAttribute returns "null (attribute is removed) if the property value is null or undefined".

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Er never mind that, i guess it would be bad to JSON.stringify(undefined)...

Comment thread docs/_guide/properties.md Outdated
```

### 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.

@kevinpschaaf kevinpschaaf Dec 18, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, that helps a lot!

@ghost
ghost merged commit a89553a into master Dec 19, 2018
@keanulee
keanulee deleted the properties-docs branch December 20, 2018 17:13
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

2 participants