-
Notifications
You must be signed in to change notification settings - Fork 319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for observers and computed properties #30
Comments
kenchris/lit-element#15 offers a way to implement own observers in a I suppose computedProperties can easily be done by doing |
We currently do not plan to add observers of computed properties. The idea is that you should generally use Our experience from The reason for omitting "computed properties" is similar. In general, you can compute necessary values for rendering directly in |
Definitely agree that listing observers in property config in PolymerElement, rather than handling it functionally, was kinda clunky. But for the use-case of reacting to properties that shouldn't cause a render, would it make sense to document the |
For properties that don't cause a render directly, setters seem to be a good replacement. I'm currently porting an app to LitElement, the app uses set route (route) {
fetch(`some/thing/${route.path}`).then(resp => {
this.someDeclaredProperty = somethingBasedOn(resp)
})
} And everything re-renders correctly after the fetch is done. |
It's okay to replace observers with setters but getters lack memorization from Polymer computed properties. For now I'm using my own decorator Gist But I think it would be great to have something similar from lit-element module @computed('firstName', 'lastName')
get fullName() {
return `${this.firstName} ${this.lastName}`.trim();
} |
probably best to use e.g.
|
actually, I was soooo wrong 🙈
I could not find a public api that allows for sync Opened a new issue #643 |
You can achieve something similar to Polymer with this mixin: const PropertyComputing = (base) => class extends base {
updated(changedProperties) {
super.updated(changedProperties);
Object.entries(this.constructor.properties)
.filter(([key, value]) => typeof value.compute === 'string')
.forEach(([key, value]) => {
if(value.deps.some(k => changedProperties.has(k))) {
this[key] = this[value.compute](...value.deps.map(dep => this[dep]))
}
})
}
}
class MyElement extends PropertyComputing(LitElement) {
static get properties() {
return {
prop1: { type: Number },
prop2: { type: Number },
comp: { type: Number, deps: ['prop1', 'prop2'], compute: '_computeComp'},
comp2: { type: Number, deps: ['prop1', 'comp'], compute: '_computeComp2'},
};
} |
As an extension to @cristinecula 's post: It's not feature complete (no support for wildcards/paths) but it did make a sample component very convenient to migrate: Neovici/cosmoz-treenode@v3.0.1...v4.0.1#diff-4dfb1d30ec99074e5e06658034dd2775 The implementation is very compact (https://github.com/Neovici/computing-lit-element/blob/master/src/computingMixin.js#L43) and works like this: This all can be conveniently packaged in a mixin so the lit-elements that needs this functionality can just, well, mix it in. We are looking into doing the same for Hope this is useful for people ending up here :) |
Will there be support for observers and computed properties as described in https://www.polymer-project.org/2.0/docs/devguide/observers? (They do not seem to be currently supported)
The text was updated successfully, but these errors were encountered: