Comments (16)
Welcome to 2018, everyone is writing his own lit-element
implementation 😀
from lit-element.
@web-padawan Could you please explain what's wrong with two-way data binding (upward data flow)? Two-way data binding was (is) my favorite feature of Polymer. What are the advantages of unidirectional data flow?
from lit-element.
This is the expected behaviour since you aren't using Polymer's templating system with lit-element, you are using lit-html's, which doesn't support 2-way binding for performance reasons. [[ ]]
and {{ }}
also don't mean anything to lit-html. The correct way to pass data upwards in lit (and in other one way systems like React), is to use events. In your case, you should listen for on-checked-changed
on the paper toggle, and then create a handler that sets the toggled
property to the value you get from the event.
from lit-element.
For completion. Here is the working element
import { LitElement, html } from '../node_modules/@polymer/lit-element/lit-element.js'
import "../node_modules/@polymer/paper-toggle-button/paper-toggle-button.js";
export class TestPageTwo extends LitElement {
constructor() {
super();
this.toggled = false;
}
// properties, observers, etc. are identical to 2.x
static get properties() {
return {
toggled: Boolean
}
}
onCheckedChanged(e) {
this.toggled = e.detail.value;
}
render({toggled}) {
return html`
${toggled}
<paper-toggle-button checked="${toggled}" on-checked-changed="${this.onCheckedChanged.bind(this)}"></paper-toggle-button>
`;
}
}
customElements.define('test-page-two', TestPageTwo);
from lit-element.
@BrandiATMuhkuh I believe _render
should be used instead of render
in your last code. 😉
from lit-element.
thank you that's good to know. I was expecting '[[ ]]' to work because this element is part of PolymerLabs. However, since polymer 3 is completely written in JS it's a much better approach to use listeners :)
from lit-element.
I tried the approach with the event listener but it seems to not be able to keep the right scope. Which means, the this
word in the event listener references the element that created the event, but not the polymer element.
I have also tried to add the event listener to the render({toggled, onCheckedChanged}
but no luck.
import { LitElement, html } from '../node_modules/@polymer/lit-element/lit-element.js'
import "../node_modules/@polymer/paper-toggle-button/paper-toggle-button.js";
export class TestPageTwo extends LitElement {
constructor() {
super();
this.toggled = false;
}
// properties, observers, etc. are identical to 2.x
static get properties() {
return {
toggled: Boolean
}
}
onCheckedChanged(e) {
this.set("toggled", e.detail.value);
}
render({toggled, onCheckedChanged}) {
return html`
${toggled}
<paper-toggle-button checked="${toggled}" on-checked-changed="${this.onCheckedChanged}"></paper-toggle-button>
`;
}
}
customElements.define('test-page-two', TestPageTwo);
from lit-element.
<paper-toggle-button checked="${toggled}" on-checked-changed="${this.onCheckedChanged.bind(this)}"></paper-toggle-button>
from lit-element.
Just bind it either before or when you attach it.
"this" is supposed to be the element on which the event is fired... so paper-toggle-button, so if you don't want that, rebind it.
You could also do it in the constructor
this.onCheckedChanged = this.onCheckedChanged.bind(this)
from lit-element.
Wonderful. Thanks you. I thought I need to you .bind(this)
, but didn't know how. Now it works.
Q: Will the API for that change in the future to automate the .bind()
process?
from lit-element.
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>
.
from lit-element.
What about something like Polymer's notify
(automatic property change notification events) to make upward data flow easier?
from lit-element.
@FluorescentHallucinogen this is the new reality, where no upward data flow is supposed to be always kept in mind. Still the Web Component should produce events, but I guess it would make sense to make the switch
component more aligned with the native input and produce a change
event.
from lit-element.
I've found https://github.com/DiiLord/lit-element and it has notify
.
@justinfagnani @DiiLord What about to port it to this element?
from lit-element.
lit-html-brackets also looks very interesting.
from lit-element.
HTML Template Instantiation proposal also adopts mustache syntax as standardized template language.
from lit-element.
Related Issues (20)
- Typescript error on latest build HOT 1
- mixins don't work when using ESNext in tsconfig to compile HOT 1
- Error throw when accessing queryAssignedNodes getter (No version >2.4.0) HOT 1
- Support for contexts? HOT 2
- Persistance? HOT 5
- idempotent/soft customElement decorator HOT 1
- state() is shown as deprecated in LitElement 2.5 HOT 1
- Should not be necessary to copy TemplateResult
- jsdelivr/+esm: [object Object] HOT 1
- Nested components, slot and text styling HOT 1
- Git tags are not in sync with npm tags HOT 2
- No release major/minor branches available for reference. HOT 2
- Move tests and benchmarks to GitHub Actions
- Accessibility => lit-element + Google Chrome Screen Reader Extension => UI Design is Changed HOT 3
- Property does not re-render when fired from custom event HOT 4
- Add a way to know when rendering of an element, and ALL its sub-elements, is finished HOT 3
- Litelement Boolean property returns undefined in when not set HOT 6
- Forward Compatibility With Lit 2 misleading HOT 2
- Starter projects missing in getting started documentation HOT 1
- How do you get the actual click event target? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lit-element.