question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement property change notification events (notify)

See original GitHub issue

What about something like Polymer’s notify (automatic property change notification events) to make upward data flow easier?

Problem:

lit-element (lit-html) doesn’t support 2-way binding. The correct way to pass data upwards is to use events.

E.g. for <paper-toggle-button> I can listen on-checked-changed, and then create a handler that sets the toggled property to the value I get from the event:

static get properties() {
  return {
    toggled: Boolean
  }
}

_render({toggled}) {
  return html`
    <paper-toggle-button
        checked="${toggled}"
        on-checked-changed="${this.onCheckedChanged.bind(this)}">
    </paper-toggle-button>

    ${toggled}
  `;
}

onCheckedChanged(e) {
  this.toggled = e.detail.value;
}

But what if I want to use <mwc-switch> (which is also based on lit-element) instead of <paper-toggle-button>?

The problem is that <mwc-switch> has no the checked-changed custom event unlike <paper-toggle-button>.

(The fact that <mwc-switch> has no the checked-changed custom event, shows that material-web-components violate Property Change Events rule of the The Gold Standard Checklist for Web Components.)

I’ve found https://github.com/DiiLord/lit-element and it has notify.

What about to port it to this element? It helps implement property change notification events as easy as adding notify to property in static get properties().

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
TimvdLippecommented, Jun 1, 2018

(The fact that <mwc-switch> has no the checked-changed custom event, shows that material-web-components violate Property Change Events rule of the The Gold Standard Checklist for Web Components.)

I would actually say: mwc-switch should start firing the event, rather than making any update to LitElement.

2reactions
pshihncommented, Jun 6, 2018

@pitus that’s because some (and custom) events do not cross the shadow dom boundary by default. You need to set composed to true.

const event = new CustomEvent('my-event', { bubbles: true, composed: true });
this.dispatchEvent(event);
Read more comments on GitHub >

github_iconTop Results From Across the Web

INotifyPropertyChanged.PropertyChanged Event
The PropertyChanged event can indicate all properties on the object have changed by using either null or String.Empty as the property name in...
Read more >
c# - Raise an event whenever a property's value changed?
The INotifyPropertyChanged interface is implemented with events. The interface has just one member, PropertyChanged , which is an event that ...
Read more >
WPF and Notifying Property Change - DaedTech
This interface consists of just a single event, and properties fire the event when set. This is the mechanism by which the model...
Read more >
INotifyPropertyChanged Is Obsolete - CODE Magazine
A PropertyChanged event is going to be handled immediately; consumers aren't going to wait until all changes are complete. If one user action ......
Read more >
Property Change Notification | Fundamental Concepts in ...
Implementing INotifyPropertyChanged : An Alternative Approach · The application's Dispatcher automatically raises the PropertyChanged event on the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found