views:

168

answers:

1

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  A: 

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.

Henning
I would of wanted to avoid a fake form submit. Do you have any other ideas or should I change my approach altogether?
ponzao
@Ponzao: You probably could still use the context approach if you did not bind a context to the FormInjector, and then modified the event URL stored somewhere inside it... but it wouldn't be pretty.A better option is probably to use two different forms: one that is submitted to load the Zone contents, and the other is inside the Zone entirely. If that's possible in your case, it is the way to go.
Henning
@Henning Ok thanks, I don't yet know what path to take but your answer gives me enough choices to do so.
ponzao