You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by bhorvat <ho...@gmail.com> on 2012/07/15 02:33:28 UTC

Injection javascript code - AjaxResponseRenderer vs JavaScriptSupport

I have tired to inject the javascript code like this

    @Inject
    private AjaxResponseRenderer ajaxResponseRenderer;

    @AfterRender
    public void set(){
        ajaxResponseRenderer.addCallback(new JavaScriptCallback() {

            @Override
            public void run(JavaScriptSupport javascriptSupport) {
                javascriptSupport.addScript("alert(%s)",
selectedComponent.getId());
            }
        })
    }

but for some reason (anyone know which one?) this didnt work

This on the other had 

    @Environmental
    private JavaScriptSupport javascriptSupport;

    @AfterRender
    public void set() {
        if (selectedComponent != null) {
            javascriptSupport.addScript("selectComponent(itemComponent%s)",
selectedComponent.getId());
        }
    }

is working. So my question is can I use the first approach in same way
(would like to avoid setting up a new variable in the class if possible). 

I think that first apprach has worked when I tried it before but at that
time it was in the ajax request, so in the method that handled the ajax
response. Now I am just refreshing the zone in which there is a component
that has that after render method. 

Anyhow what should be the best way for this? 

Cheers and tnx all

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Injection-javascript-code-AjaxResponseRenderer-vs-JavaScriptSupport-tp5714464.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Injection javascript code - AjaxResponseRenderer vs JavaScriptSupport

Posted by bhorvat <ho...@gmail.com>.
My scenario is that I have a link (there is zone parameter) that refreshes
the zone. Now in this zone there is a component in which there is a method 

    @AfterRender 
    public void set(){ 
        ajaxResponseRenderer.addCallback(new JavaScriptCallback() { 

            @Override 
            public void run(JavaScriptSupport javascriptSupport) { 
                javascriptSupport.addScript("alert(%s)",
selectedComponent.getId()); 
            } 
        }) 
    } 

Now this method gets triggered but the run part is never actually triggered.
If I use the JavaScriptSupport  instead of the AjaxResponseRenderer it works
without a problem. But I would expect that the first approach works just as
well. 

I did try this approach in the method that gets triggered for the ajax
request and then this works but my problem is that this part needs to be in
the component and then I need to use (right?) @AfterRender. 

tnx all

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Injection-javascript-code-AjaxResponseRenderer-vs-JavaScriptSupport-tp5714464p5714468.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Injection javascript code - AjaxResponseRenderer vs JavaScriptSupport

Posted by Lance Java <la...@googlemail.com>.
AjaxResponseRenderer is only available for Ajax requests. JavaScriptSupport
is available for non Ajax requests.

I'm guessing either you didn't specify a zone on your eventlink / form or
you have a JavaScript error on your page (meaning tapestry can't decorate
your links with Ajax actions)

On Sunday, 15 July 2012, bhorvat <ho...@gmail.com> wrote:
> I have tired to inject the javascript code like this
>
>     @Inject
>     private AjaxResponseRenderer ajaxResponseRenderer;
>
>     @AfterRender
>     public void set(){
>         ajaxResponseRenderer.addCallback(new JavaScriptCallback() {
>
>             @Override
>             public void run(JavaScriptSupport javascriptSupport) {
>                 javascriptSupport.addScript("alert(%s)",
> selectedComponent.getId());
>             }
>         })
>     }
>
> but for some reason (anyone know which one?) this didnt work
>
> This on the other had
>
>     @Environmental
>     private JavaScriptSupport javascriptSupport;
>
>     @AfterRender
>     public void set() {
>         if (selectedComponent != null) {
>
javascriptSupport.addScript("selectComponent(itemComponent%s)",
> selectedComponent.getId());
>         }
>     }
>
> is working. So my question is can I use the first approach in same way
> (would like to avoid setting up a new variable in the class if possible).
>
> I think that first apprach has worked when I tried it before but at that
> time it was in the ajax request, so in the method that handled the ajax
> response. Now I am just refreshing the zone in which there is a component
> that has that after render method.
>
> Anyhow what should be the best way for this?
>
> Cheers and tnx all
>
> --
> View this message in context:
http://tapestry.1045711.n5.nabble.com/Injection-javascript-code-AjaxResponseRenderer-vs-JavaScriptSupport-tp5714464.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Injection javascript code - AjaxResponseRenderer vs JavaScriptSupport

Posted by Paul Stanton <pa...@mapshed.com.au>.
Just further to this, here is the utility service I use to wrap JS calls 
from any context (which helps leverage re-usable code).

ie using this avoids having to change between AjaxResponseRenderer and 
JavaScriptSupport depending on the context (XHR/phase).

public class JavaScriptHelperImpl implements JavaScriptHelper
{
     @Inject
     private Environment env;
     @Inject
     private AjaxResponseRenderer ajaxResponseRenderer;
     @Inject
     private Request request;
     @Inject
     private JavaScriptSupport jsSupport;

     @Override
     public void addScript(final InitializationPriority priority, final 
String format, final Object... args)
     {
         if (!request.isXHR()
         // test environment for existence of JS support - ie only use 
AjaxResponseRenderer if xhr AND not render phase
                 || 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);
             }
         });
     }
}

On 16/07/2012 8:11 AM, Paul Stanton wrote:
> As Lance said, you need to use JavascriptSupport when preparing a 
> non-ajax response, and AjaxResponseRenderer when you are...
>
> if (request.isXHR())
> {
>     // use AjaxResponseRenderer
> }
> else
> {
>     // use JavascriptSupport
> }
>
> however if the bug (https://issues.apache.org/jira/browse/TAP5-1870 ) 
> affects your case too then afterrender is not being triggered 
> automatically by the framework and you need to execute the method 
> (call set yourself) from within your component event handler...
>
> hope that helps, paul.
>
> On 15/07/2012 11:17 PM, bhorvat wrote:
>> Hi Paul,
>>
>> Hm... yea this appears to be the same problem that I am having. The set
>> method gets called but the run part doesn't. In other words the 
>> methods is
>> called and the callback is added to the AjaxResponseRenderer but it 
>> is never
>> triggered (the add script part in the run method that is)
>>
>> -- 
>> View this message in context: 
>> http://tapestry.1045711.n5.nabble.com/Injection-javascript-code-AjaxResponseRenderer-vs-JavaScriptSupport-tp5714464p5714469.html
>> Sent from the Tapestry - User mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Injection javascript code - AjaxResponseRenderer vs JavaScriptSupport

Posted by Paul Stanton <pa...@mapshed.com.au>.
As Lance said, you need to use JavascriptSupport when preparing a 
non-ajax response, and AjaxResponseRenderer when you are...

if (request.isXHR())
{
     // use AjaxResponseRenderer
}
else
{
     // use JavascriptSupport
}

however if the bug (https://issues.apache.org/jira/browse/TAP5-1870 ) 
affects your case too then afterrender is not being triggered 
automatically by the framework and you need to execute the method (call 
set yourself) from within your component event handler...

hope that helps, paul.

On 15/07/2012 11:17 PM, bhorvat wrote:
> Hi Paul,
>
> Hm... yea this appears to be the same problem that I am having. The set
> method gets called but the run part doesn't. In other words the methods is
> called and the callback is added to the AjaxResponseRenderer but it is never
> triggered (the add script part in the run method that is)
>
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/Injection-javascript-code-AjaxResponseRenderer-vs-JavaScriptSupport-tp5714464p5714469.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Injection javascript code - AjaxResponseRenderer vs JavaScriptSupport

Posted by bhorvat <ho...@gmail.com>.
Hi Paul,

Hm... yea this appears to be the same problem that I am having. The set
method gets called but the run part doesn't. In other words the methods is
called and the callback is added to the AjaxResponseRenderer but it is never
triggered (the add script part in the run method that is)

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Injection-javascript-code-AjaxResponseRenderer-vs-JavaScriptSupport-tp5714464p5714469.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Injection javascript code - AjaxResponseRenderer vs JavaScriptSupport

Posted by Paul Stanton <pa...@mapshed.com.au>.
i'm assuming this 'afterrender' is within a component which is within a 
zone being updated via an ajax request/response?

if so, is this the same as the issue i logged?:

https://issues.apache.org/jira/browse/TAP5-1870

does your "set" method get called at all?

On 15/07/2012 10:33 AM, bhorvat wrote:
> I have tired to inject the javascript code like this
>
>      @Inject
>      private AjaxResponseRenderer ajaxResponseRenderer;
>
>      @AfterRender
>      public void set(){
>          ajaxResponseRenderer.addCallback(new JavaScriptCallback() {
>
>              @Override
>              public void run(JavaScriptSupport javascriptSupport) {
>                  javascriptSupport.addScript("alert(%s)",
> selectedComponent.getId());
>              }
>          })
>      }
>
> but for some reason (anyone know which one?) this didnt work
>
> This on the other had
>
>      @Environmental
>      private JavaScriptSupport javascriptSupport;
>
>      @AfterRender
>      public void set() {
>          if (selectedComponent != null) {
>              javascriptSupport.addScript("selectComponent(itemComponent%s)",
> selectedComponent.getId());
>          }
>      }
>
> is working. So my question is can I use the first approach in same way
> (would like to avoid setting up a new variable in the class if possible).
>
> I think that first apprach has worked when I tried it before but at that
> time it was in the ajax request, so in the method that handled the ajax
> response. Now I am just refreshing the zone in which there is a component
> that has that after render method.
>
> Anyhow what should be the best way for this?
>
> Cheers and tnx all
>
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/Injection-javascript-code-AjaxResponseRenderer-vs-JavaScriptSupport-tp5714464.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org