区域组件更改 Tapestry 5 中其他组件的 ID

发布于 2024-12-22 11:32:23 字数 164 浏览 7 评论 0原文

我在区域内有一个 from 和其他组件。每当区域刷新时,它也会更改表单组件和其他组件的 ID。我在 JavaScript 中使用组件的 id,因此面临问题,因为区域更改了 ID。

有什么方法可以阻止 Tapestry 5 中区域的这种行为吗?

提前感谢各位。

问候,

I have a from and other components inside the Zone. whenever Zone get refreshed it change the ID of form component and other components too. I am using the id of components in JavaScript so facing problem because Zone changes the IDs.

is there any way to stop this behavior of Zone in Tapestry 5.

Thanks in Advance guys.

Regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琉璃繁缕 2024-12-29 11:32:23

简而言之,不。当从 AJAX 请求返回内容时,根据设计,ID 可以是任何内容。

更长的答案是,您可能应该以不同的方式构建代码,并根据区域的内容创建一个组件:

<div t:type="Zone" id="zone" t:id="zone">
    <div t:type="MyZoneContent" t:id="myZoneContent" ... />
</div>

然后,确保使用该新组件中的实际客户端 ID 初始化 JS:

public MyZoneContent implements ClientElement {

    @Environmental
    private JavaScriptSupport renderSupport;

    /**
     * An id for the component. If not specified, a default is generated.
     */
    @Parameter(required = false, defaultPrefix = BindingConstants.LITERAL )
    private String idParameter;

    private String clientId;

    @BeginRender
    void setupClientId() {
        this.clientId = resources.isBound("id") ? idParameter :
               renderSupport.allocateClientId(resources);
    }

    @AfterRender
    void addScript() {
       this.renderSupport.addScript("new MyJavascriptClass('%s');",
               this.getClientId());
    }

    @Override
    public String getClientId() {
        return this.clientId;
    }

}

的模板:

<div id="${clientId}" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
   <form t:type="Form" ...>
      ...
   </form>
</div>

以及新组件 这样,您将组件的实际客户端ID传递给Javascript初始化程序,从而在每次重新加载区域内容时重新初始化JS。

In short, no. When content is returned from an AJAX request, by design, the IDs can be anything.

The longer answer is that you should possibly structure your code differently and create a component from the contents of the Zone:

<div t:type="Zone" id="zone" t:id="zone">
    <div t:type="MyZoneContent" t:id="myZoneContent" ... />
</div>

Then, make sure you initialize your JS with the actual client ID in that new component:

public MyZoneContent implements ClientElement {

    @Environmental
    private JavaScriptSupport renderSupport;

    /**
     * An id for the component. If not specified, a default is generated.
     */
    @Parameter(required = false, defaultPrefix = BindingConstants.LITERAL )
    private String idParameter;

    private String clientId;

    @BeginRender
    void setupClientId() {
        this.clientId = resources.isBound("id") ? idParameter :
               renderSupport.allocateClientId(resources);
    }

    @AfterRender
    void addScript() {
       this.renderSupport.addScript("new MyJavascriptClass('%s');",
               this.getClientId());
    }

    @Override
    public String getClientId() {
        return this.clientId;
    }

}

And the template for the new component:

<div id="${clientId}" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
   <form t:type="Form" ...>
      ...
   </form>
</div>

That way, you're passing the actual client ID of your component to your Javascript initializer, thus re-initializing the JS every time the content of your Zone is reloaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文