You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Joe Chen <jo...@gmail.com> on 2006/09/24 20:33:26 UTC

How to execute javascript from EventListener (T4.1)

Does anybody have an example of how to execute javascript from an
EventListener in Tapestry 4.1?  I'd like to implement the Dialog from
tacos in T4.1 and also implement some of the tacos effects before
updating a component.

For instance, In tacos, in order to hide the dialog, a listener would
call the hide method, which looks like this:

public void hide()
    {
        //if (isHidden()) return;
        setHidden(true);
        AjaxWebRequest ajaxr = getAjaxRequest();
        if (ajaxr != null && ajaxr.isValidRequest()) {
            ajaxr.addPreProcessScript(generateHide(true));
            ajaxr.addPostProcessScript(initializeScript());
        }
    }

/**
     * Creates the javascript that will hide the dialog widget.
     * @return
     */
    protected String generateHide(boolean renderHide)
    {
        StringBuffer str = new StringBuffer();
        str.append("<script>\n");
        str.append("var cdialog = dojo.widget.byId(\"")
        .append(getId()).append("\");\n")
        .append("if (cdialog) {\n");
        str.append(" var ndialog = document.createElement(\"div\");\n")
        .append("ndialog.setAttribute(\"id\", \"").append(getId())
        .append("\");\n");
        str.append(" ndialog.style.display = \"none\";");
        str.append(" document.body.appendChild(ndialog);\n");
        if (renderHide) {
            str.append(" cdialog.hide(); \n");
        }
        str.append("}");
        str.append("</script>");
        return str.toString();
    }

What is the equivalent of AjaxWebRequest.addPreProcessScript() in Tapestry 4.1?

Thanks!
Joe

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


Re: How to execute javascript from EventListener (T4.1)

Posted by Jesse Kuhnert <jk...@gmail.com>.
Yes, sorry for this. I haven't formalized a plan yet for telling people how
to write javascript back in responses as I'm unsure of the API still.
(though that will change in the next couple of weeks as I start adding a
crapload of new components)

The @Script template can be used to handle ajax responses as well as normal
responses. For instance, let's say you have a @Script template like this:

<body>
   function myFunction(setupString){
      ///do stuff with it
   }
</body>
<initialization>
  myFunction("foo");
</initialization>

In a normal response the script specified in body will be executed
immediately, but before any script in the initialization block.

The initialization javscript will be executed on window load. (The semantics
of window load have changed dramatically in the latest svn head of tapestry,
which hasn't been released to snapshot builds yet because of pending date
i18n changes - but the overall gist is that we now use
http://ajaxian.com/archives/domcontentloaded-gains-traction-now-in-dojo from
Dean Edwards great work on the subject.....It basically boils down to
dramatically faster page load times )

Now take the same template, when you execute it during an ajax response the
body script will still be executed in logically the same exact place as
before...Which is before the initialization block..The intialization block
will also be guaranteed to execute after the body script. (.js includes in
@Script template are all handled properly of course as well).

That is one way, the way that it looks like your example wants to do it (and
which I'm still guessing easier / more formalized API calls will be created
to handle) is something like:

public void foo(IRequestCycle cycle){
   cycle.getResponseBuilder().getWriter(
ResponseBuilder.INITIALIZATION_SCRIPT,
ResponseBuilder.SCRIPT_TYPE).printRaw("your
javascript string here");
}

See the javadocs for more explanation:
http://tapestry.apache.org/tapestry4.1/tapestry-framework/apidocs/org/apache/tapestry/services/ResponseBuilder.html#getWriter(java.lang.String,%20java.lang.String)

Sorry again for the lack of documentation on this subject..It's partially
done on purpose but I also didn't expect to get slammed with so much work
before finishing out some of the API related to these kinds of tasks. It
will be done soon.

On 9/24/06, Joe Chen <jo...@gmail.com> wrote:
>
> Does anybody have an example of how to execute javascript from an
> EventListener in Tapestry 4.1?  I'd like to implement the Dialog from
> tacos in T4.1 and also implement some of the tacos effects before
> updating a component.
>
> For instance, In tacos, in order to hide the dialog, a listener would
> call the hide method, which looks like this:
>
> public void hide()
>     {
>         //if (isHidden()) return;
>         setHidden(true);
>         AjaxWebRequest ajaxr = getAjaxRequest();
>         if (ajaxr != null && ajaxr.isValidRequest()) {
>             ajaxr.addPreProcessScript(generateHide(true));
>             ajaxr.addPostProcessScript(initializeScript());
>         }
>     }
>
> /**
>      * Creates the javascript that will hide the dialog widget.
>      * @return
>      */
>     protected String generateHide(boolean renderHide)
>     {
>         StringBuffer str = new StringBuffer();
>         str.append("<script>\n");
>         str.append("var cdialog = dojo.widget.byId(\"")
>         .append(getId()).append("\");\n")
>         .append("if (cdialog) {\n");
>         str.append(" var ndialog = document.createElement(\"div\");\n")
>         .append("ndialog.setAttribute(\"id\", \"").append(getId())
>         .append("\");\n");
>         str.append(" ndialog.style.display = \"none\";");
>         str.append(" document.body.appendChild(ndialog);\n");
>         if (renderHide) {
>             str.append(" cdialog.hide(); \n");
>         }
>         str.append("}");
>         str.append("</script>");
>         return str.toString();
>     }
>
> What is the equivalent of AjaxWebRequest.addPreProcessScript() in Tapestry
> 4.1?
>
> Thanks!
> Joe
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Jesse Kuhnert
Tapestry/Dojo/(and a dash of TestNG), team member/developer

Open source based consulting work centered around
dojo/tapestry/tacos/hivemind. http://blog.opencomponentry.com