[ 
https://issues.apache.org/jira/browse/TAP5-1861?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13720180#comment-13720180
 ] 

Paul Stanton commented on TAP5-1861:
------------------------------------

Hi Howard,

I've made some discoveries which make the implementation posted here out of 
date; i'm sure you are aware of the following:

1. if in the render or event handling phase of a normal request, 
JavaScriptSupport must be used over AjaxResponseRenderer
2. if in the event handling phase of an ajax request AjaxResponseRenderer must 
be used over JavaScriptSupport
however this was news to me:
3. if in the 'render phase' of an ajax request, JavaScriptSupport must be used 
over AjaxResponseRenderer

.. and in this third case, the 'isXHR' test is not sufficient; the only way to 
determine this case is to 'peek' for JavaScriptSupport.

therefore, my current implementation of the previously posted utility looks 
like this:

        public void addScript(final InitializationPriority priority, final 
String format, final Object... args)
        {
                if (!request.isXHR()
                                || env.peek(JavaScriptSupport.class) != null)
                {
                        jsSupport.addScript(priority, format, args);
                        return;
                }

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

I hope this helps design whatever solution you come up with.

Regards, Paul.
                
> Wrap javascript call rendering apis for interoperability and convenience
> ------------------------------------------------------------------------
>
>                 Key: TAP5-1861
>                 URL: https://issues.apache.org/jira/browse/TAP5-1861
>             Project: Tapestry 5
>          Issue Type: Improvement
>          Components: tapestry-core
>    Affects Versions: 5.3.2
>            Reporter: Paul Stanton
>            Assignee: Howard M. Lewis Ship
>              Labels: ajax, javascript
>
> Vast improvements to the ajax and javascript rendering API's have made things 
> much easier to use in tapestry 5.2+, however I still think there is room for 
> improvement. I find I can't work without this 'helper' service I designed to 
> standardise script call rendering between xhr and non-xhr requests. The main 
> motivation/benefit is that a set of functionality can now be called in either 
> (xhr/non-xhr) context and still work. It also has the benefit of building the 
> necessary JavaScriptCallback in the xhr context. In my opinion, this makes 
> user code much tidier:
> public class JavascriptHelperImpl implements JavascriptHelper
> {
>     @Inject
>     private AjaxResponseRenderer ajaxResponseRenderer;
>     @Inject
>     private Request request;
>     @Inject
>     private JavaScriptSupport jsSupport;
>     @Override
>     public void addScript(final String format, final Object... args)
>     {
>         if (!request.isXHR())
>         {
>             jsSupport.addScript(format, args);
>             return;
>         }
>         ajaxResponseRenderer.addCallback(new JavaScriptCallback()
>         {
>             @Override
>             public void run(JavaScriptSupport javascriptSupport)
>             {
>                 javascriptSupport.addScript(format, args);
>             }
>         });
>     }
> }
> There may or may not be additional opportunity with the other methods such as 
> 'addInitializerCall' but I haven't needed to.
> This code is free for anyone (including apache/tapestry) to use.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to