简体   繁体   English

Tapestry 事件动态组件

[英]Tapestry event dynamic components

I want to implement an eventhandler for a component, which is dynamically generated.我想为动态生成的组件实现一个事件处理程序。 That means I have a Table , whichs source is bound to a custom TableModel (which implements an interface JsonGenerator , with which the json data can be generated).这意味着我有一个Table ,它的源绑定到一个自定义的TableModel (它实现了一个接口JsonGenerator ,可以使用它生成json数据)。 In this TableModel , I add a few custom components, which fire an event ( valueChanged ) when their state changes.在这个TableModel ,我添加了一些自定义组件,当它们的状态发生变化时会触发一个事件 ( valueChanged )。 Those components have ids (1 to 4).这些组件具有 ID(1 到 4)。 However, I can't just add a method similiar to this:但是,我不能只添加与此类似的方法:

void onValueChangedFrom1 ()

as there are times when no component with the id 1 exists.因为有时不存在 ID 为 1 的组件。 I'd get the following exception when trying anway:尝试 anway 时,我会遇到以下异常:

Method de.[...].onValueChangedFrom1() references component id '1' which does not exist.

I can't modify the custom TableModel , the JsonGenerator nor the other custom components.我无法修改自定义TableModelJsonGenerator或其他自定义组件。

It might be interesting to know that the values of the custom components can be accessed via the Request service of tapestry like that:知道自定义组件的值可以通过挂毯的Request服务访问可能会很有趣,如下所示:

request.getParameter("1");

But how can I add an event handler when this value changes?但是当这个值改变时我如何添加一个事件处理程序? Is this even possible in Tapestry?这在 Tapestry 中甚至是可能的吗?

You can add the 1-4 as context on the event and capture it in your event handler.您可以将 1-4 添加为事件的上下文并在事件处理程序中捕获它。 Your event handler needs to be component agnostic though, meaning you need an event handler for all, ie不过,您的事件处理程序需要与组件无关,这意味着您需要一个适合所有人的事件处理程序,即

public void onValueChanged(@RequestParameter("newValue") Object newValue, int context) {
    ...
}

Then on each of your custom components add the context to the event, you did not show how you are currently generating the event so I am not able to tell you what to update.然后在您的每个自定义组件上将上下文添加到事件中,您没有显示您当前如何生成事件,因此我无法告诉您要更新的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM