Modifying FormInjector context information in Tapestry 5 dynamically

折月煮酒 提交于 2020-01-02 09:26:08

问题


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.

I added the following in my template.

<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;
}

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.


回答1:


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.)

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.



来源:https://stackoverflow.com/questions/2974282/modifying-forminjector-context-information-in-tapestry-5-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!