在Tapestry 5中修改FormInjector上下文信息动态上下文、动态、信息、Tapestry

2023-09-10 18:00:16 作者:陌上轻尘

我现在的问题动态关于在 FormInjector 更新方面的信息,我的previous问题Updating在挂毯5 一个表单内的区域可能包含有用的背景资料。

My current problem regards updating context information dynamically in FormInjector, my previous question Updating a zone inside a form in Tapestry 5 probably contains useful background information.

我说在我的模板下面。

<div t:type="FormInjector" t:id="injector" t:context="item.id"/>

而在我的组件类以下。

And the following in my component class.

@OnEvent(component = "injector")
Block loadItemFields(String id) {
    item = itemRepository.find(id);
    return itemFieldsBlock;
}

一切工作正常,新的表单字段出现,但是搜索总是以相同 ID 完成。我想触发事件之前修改 ID 用JavaScript,但我不知道如何实现这一点。

Everything is working fine, new form fields appear, but the search is always done with the same id. I would like to change the id with JavaScript before triggering the event, but I don't know how to achieve this.

如果有需要的其他信息我很高兴地提供它。

If there is additional information required I am happy to supply it.

推荐答案

使用上下文参数传递一个动态值不会是我的第一选择。 (该 FormInjector 组件生成的URL来触发事件处理程序,然后包括上下文 - 然而,这样做是当组件呈现,并且不意味着是动态的。 )

Using the context parameter to pass a dynamic value wouldn't be my first option. (The FormInjector component generates a URL to trigger the event handler, which then includes the context - however, this is done when the component renders, and is not meant to be dynamic.)

我想摆脱的背景下的参数,并找到一种不同的方式来提交的价值。一种可能性是通过AJAX提交表单并触发注射回调:

I'd get rid of the context parameter and find a different way to submit the value. One possibility would be to submit the form via AJAX and trigger the injection in the callback:

this.myFormElement.observe('change', this.onChange.bindAsEventListener(this));

...

onChange: function(event) {
    this.myFormElement.form.request({
           onSuccess: this.afterFormSubmitted.bind(this)
    });
},

afterFormSubmitted: function() {
   this.formInjector.trigger();
}

这样,表单元素的值被设置在服务器端,当你触发形式注入的,你可以在注射的事件处理程序使用它。

That way, the value of the form element has been set on the server side when you trigger the form injection, and you can use it in your injection event handler.