通知服务器端,一种抽象的propertyfield,属性在litelement内部使用vaadin Flow变化
至少至少Vaadin流23官方组件是polymer3(来自 我看到的),基本上是贬低了赞成点燃的。
给定服务器端AppractsinglePropertyField
(请参见下文), 这包含一个简单的复选框,应该“镜像”属性 从客户端拨打<代码>检查。
然后,服务器端听从 客户,Polymer3很乐意为这种属性开火。
现在,考虑使用LIT:LIT的使用WebComponent的使用:
import {LitElement, html} from "lit-element";
export class MyCheckBox extends LitElement {
static get properties() {
return {checked: Boolean};
}
render() {
return html`<label><input type="checkbox" ?checked=${this.checked} @click=${this.toggleChecked}/>Toggle</label>`
}
toggleChecked(e) {
this.checked = e.target.checked;
}
}
customElements.define('my-checkbox', MyCheckBox);
不再自动触发检查已更改的
事件。
那么,官方/轻松/...处理客户端属性的方式是什么 更改并通知服务器(期望“ polymer3式”) 他们
现在
import {LitElement, html} from "lit-element";
export class MyCheckBox extends LitElement {
// ...
update(_changedProperties) {
super.update(_changedProperties);
this.fireChanged(_changedProperties, 'checked'); // XXX
}
fireChanged(_changedProperties, property) {
if (_changedProperties.has(property)) {
let htmlChangedEvent = new CustomEvent(property.concat("-changed"), {
detail: {
propertyName: property,
value: this.html,
oldValue: _changedProperties.get(property),
userOriginated: true
}
});
this.dispatchEvent(htmlChangedEvent);
}
}
}
customElements.define('my-checkbox', MyCheckBox);
的
@Tag('my-checkbox')
@JsModule('./my-checkbox.js')
class MyCheckbox extends AbstractSinglePropertyField<MyCheckbox, Boolean> {
MyCheckbox() {
super('checked', false, false)
}
}
?
@Route("")
class MyForm extends Div {
MyForm() {
def mcb = new MyCheckbox().tap{
addValueChangeListener{
Notification.show("Value changed to ${it.value}")
}
}
add(mcb)
}
}
是 检查已更改的
事件, 通知永远不会显示。
Up to at least Vaadin Flow 23 the official components are Polymer3 (from
what I saw), which is basically deprecated in favour of Lit.
Given a server side AbstractSinglePropertyField
(see below for code),
that wraps a simple checkbox and is supposed to "mirror" a property
called checked
from the client.
The server side then listens for checked-changed
events from the
client, which Polymer3 happily fires for such a property.
Now consider the use of a webcomponent using Lit:
import {LitElement, html} from "lit-element";
export class MyCheckBox extends LitElement {
static get properties() {
return {checked: Boolean};
}
render() {
return html`<label><input type="checkbox" ?checked=${this.checked} @click=${this.toggleChecked}/>Toggle</label>`
}
toggleChecked(e) {
this.checked = e.target.checked;
}
}
customElements.define('my-checkbox', MyCheckBox);
Lit no longer automatically fires the checked-changed
event.
So what is the official/easy/... way to deal with client-side property
changes and notify the server (which expects "Polymer3-style") about
them?
As of now, as a workaround, I fire my own event:
import {LitElement, html} from "lit-element";
export class MyCheckBox extends LitElement {
// ...
update(_changedProperties) {
super.update(_changedProperties);
this.fireChanged(_changedProperties, 'checked'); // XXX
}
fireChanged(_changedProperties, property) {
if (_changedProperties.has(property)) {
let htmlChangedEvent = new CustomEvent(property.concat("-changed"), {
detail: {
propertyName: property,
value: this.html,
oldValue: _changedProperties.get(property),
userOriginated: true
}
});
this.dispatchEvent(htmlChangedEvent);
}
}
}
customElements.define('my-checkbox', MyCheckBox);
The server side (for both client sides):
@Tag('my-checkbox')
@JsModule('./my-checkbox.js')
class MyCheckbox extends AbstractSinglePropertyField<MyCheckbox, Boolean> {
MyCheckbox() {
super('checked', false, false)
}
}
And a trivial test:
@Route("")
class MyForm extends Div {
MyForm() {
def mcb = new MyCheckbox().tap{
addValueChangeListener{
Notification.show("Value changed to ${it.value}")
}
}
add(mcb)
}
}
Without the firing of the "manual" checked-changed
event, the
notification never shows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论