通知服务器端,一种抽象的propertyfield,属性在litelement内部使用vaadin Flow变化

发布于 2025-02-12 08:43:10 字数 2292 浏览 4 评论 0原文

至少至少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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文