Author: ivaynberg
Date: Thu Jan 25 09:44:35 2007
New Revision: 499867

URL: http://svn.apache.org/viewvc?view=rev&rev=499867
Log:
WICKET-234: Adding Listener to AjaxRequestTarget

Modified:
    
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/ajax/AjaxRequestTarget.java

Modified: 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/ajax/AjaxRequestTarget.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/ajax/AjaxRequestTarget.java?view=diff&rev=499867&r1=499866&r2=499867
==============================================================================
--- 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/ajax/AjaxRequestTarget.java
 (original)
+++ 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/ajax/AjaxRequestTarget.java
 Thu Jan 25 09:44:35 2007
@@ -65,9 +65,9 @@
  * feature can be useful when it is desirable to link component update with 
some
  * javascript effects.
  * <p>
- * The target provides a listener interface [EMAIL PROTECTED] Listener} that 
can be used to
- * add code that responds to various target events by adding listeners via
- * [EMAIL PROTECTED] #addListener(wicket.ajax.AjaxRequestTarget.Listener)}
+ * The target provides a listener interface [EMAIL PROTECTED] IListener} that 
can be used
+ * to add code that responds to various target events by adding listeners via
+ * [EMAIL PROTECTED] #addListener(wicket.ajax.AjaxRequestTarget.IListener)}
  * 
  * @since 1.2
  * 
@@ -81,7 +81,7 @@
         * various target-related events
         * 
         */
-       public static interface Listener
+       public static interface IListener
        {
                /**
                 * Triggered before ajax request target begins its response 
cycle
@@ -95,6 +95,41 @@
                 * 
                 */
                public void onBeforeRespond(Map map, AjaxRequestTarget target);
+
+               /**
+                * Triggered after ajax request target is done with its 
response cycle.
+                * At this point only additional javascript can be output to the
+                * response using the provided [EMAIL PROTECTED] 
IJavascriptResponse} object
+                * 
+                * NOTE: During this stage of processing any calls to target 
that
+                * manipulate the response (adding components, javascript) will 
have no
+                * effect
+                * 
+                * @param map
+                *            read-only map:markupId->component of components 
already
+                *            added to the target
+                * @param response
+                *            response object that can be used to output 
javascript
+                */
+               public void onAfterRespond(Map map, IJavascriptResponse 
response);
+       }
+
+       /**
+        * An ajax javascript response that allows users to add javascript to be
+        * executed on the client side
+        * 
+        * @author ivaynberg
+        */
+       public static interface IJavascriptResponse
+       {
+               /**
+                * Adds more javascript to the ajax response that will be 
executed on
+                * the client side
+                * 
+                * @param script
+                *            javascript
+                */
+               public void addJavascript(String script);
        }
 
        /**
@@ -225,7 +260,7 @@
         * 
         * @param listener
         */
-       public void addListener(Listener listener)
+       public void addListener(IListener listener)
        {
                if (listener == null)
                {
@@ -393,7 +428,7 @@
                        final String encoding = 
app.getRequestCycleSettings().getResponseRequestEncoding();
 
                        // Set content type based on markup type for page
-                       WebResponse response = 
(WebResponse)requestCycle.getResponse();
+                       final WebResponse response = 
(WebResponse)requestCycle.getResponse();
                        response.setCharacterEncoding(encoding);
                        response.setContentType("text/xml; charset=" + 
encoding);
 
@@ -415,7 +450,7 @@
                                Iterator it = listeners.iterator();
                                while (it.hasNext())
                                {
-                                       
((Listener)it.next()).onBeforeRespond(components, this);
+                                       
((IListener)it.next()).onBeforeRespond(components, this);
                                }
                        }
 
@@ -437,6 +472,30 @@
                                respondInvocation(response, js);
                        }
 
+                       // invoke onafterresponse event on listeners
+                       if (listeners != null)
+                       {
+                               final Map components = 
Collections.unmodifiableMap(markupIdToComponent);
+
+                               // create response that will be used by 
listeners to append
+                               // javascript
+                               final IJavascriptResponse jsresponse = new 
IJavascriptResponse()
+                               {
+
+                                       public void addJavascript(String script)
+                                       {
+                                               respondInvocation(response, 
script);
+                                       }
+
+                               };
+
+                               it = listeners.iterator();
+                               while (it.hasNext())
+                               {
+                                       
((IListener)it.next()).onAfterRespond(components, jsresponse);
+                               }
+                       }
+
                        response.write("</ajax-response>");
                }
                catch (RuntimeException ex)
@@ -448,7 +507,6 @@
                        LOG.error("Error while responding to an AJAX request: " 
+ toString(), ex);
                }
        }
-
 
        /**
         * Processes components added to the target. This involves attaching


Reply via email to