pstanton
pstanton

Reputation: 36679

Mixin script added in afterrender not fired for xhr requests

Can someone familiar with the mixin lifecycle please advise me:

(using tapestry 5.3.6)

I have a mixin which triggers some javascript from 'afterRender'. I do this in 'afterRender' because I need the field's clientId to be initialised.

It works great if the Field it attaches to is rendered when the page is first rendered, however if it is rendered as part of a zone update/xhr request, the script is never sent to the client.

I have breakpointed the JavaScriptCallback and while it is successfully added to the ajaxResponseRenderer, it is never called.

Thanks for advice!

code Eg:

public class MyMixin
{
    ...

    void afterRender()
    {
        addScript("MyMixin.create('%s', '%s');", field.getClientId(), myVariable);
    }

    private void addScript(final String format, final Object... args)
    {
        if (!request.isXHR())
        {
            jsSupport.addScript(InitializationPriority.NORMAL, format, args);
            return;
        }

        ajaxResponseRenderer.addCallback(new JavaScriptCallback()
        {
            @Override
            public void run(JavaScriptSupport javascriptSupport)
            {
                javascriptSupport.addScript(InitializationPriority.NORMAL, format, args);
            }
        });
    }
} 

Upvotes: 0

Views: 377

Answers (1)

Vitaly
Vitaly

Reputation: 2800

You need to use the same jsSupport object in both cases: request.isXHR() == {true or false}.

JavaScriptSupport is already registered in AjaxResponseRenderer, see AjaxResponseRendererImpl.

So your code would look like

void afterRender() {

    jsSupport.addScript("MyMixin.create('%s', '%s');", field.getClientId(), myVariable);
}

Upvotes: 0

Related Questions