Modifying FormInjector context information in Tape

2019-05-19 17:05发布

问题:

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.