You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@click.apache.org by sa...@apache.org on 2010/07/17 11:54:28 UTC

svn commit: r965047 - in /click/trunk/click/examples: src/org/apache/click/examples/page/ajax/AjaxFallbackPage.java webapp/WEB-INF/menu.xml webapp/ajax/ajax-fallback.htm

Author: sabob
Date: Sat Jul 17 09:54:28 2010
New Revision: 965047

URL: http://svn.apache.org/viewvc?rev=965047&view=rev
Log:
added ajax fallback demo

Added:
    click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxFallbackPage.java
    click/trunk/click/examples/webapp/ajax/ajax-fallback.htm
Modified:
    click/trunk/click/examples/webapp/WEB-INF/menu.xml

Added: click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxFallbackPage.java
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxFallbackPage.java?rev=965047&view=auto
==============================================================================
--- click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxFallbackPage.java (added)
+++ click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxFallbackPage.java Sat Jul 17 09:54:28 2010
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.click.examples.page.ajax;
+
+import org.apache.click.ActionListener;
+import org.apache.click.Control;
+import org.apache.click.Partial;
+import org.apache.click.ajax.AjaxBehavior;
+import org.apache.click.control.ActionLink;
+import org.apache.click.examples.page.BorderPage;
+
+/**
+ * Demonstrates how to handle an AJAX request and fallback to a non-AJAX request
+ * in case JavaScript is disabled.
+ */
+public class AjaxFallbackPage extends BorderPage {
+
+    private static final long serialVersionUID = 1L;
+
+    private ActionLink link = new ActionLink("link", "here");
+
+    public AjaxFallbackPage() {
+        link.setId("link-id");
+
+        addControl(link);
+
+        // If JavaScript is enabled, the AjaxBehavior will be called
+        link.addBehavior(new AjaxBehavior() {
+
+            @Override
+            public Partial onAction(Control source) {
+                // Formatted date instance that will be added to the
+                String now = format.currentDate("MMM, yyyy dd HH:MM:ss");
+
+                String msg = "AjaxBehavior <tt>onAction()</tt> method invoked at: " + now;
+                // Return a partial containing the message
+                return new Partial(msg, Partial.HTML);
+            }
+        });
+
+        // If JavaScript is disabled, the ActionListener will be called instead
+        link.setActionListener(new ActionListener() {
+
+            public boolean onAction(Control source) {
+                // Formatted date instance that will be added to the
+                String now = format.currentDate("MMM, yyyy dd HH:MM:ss");
+
+                String msg = "ActionListener <tt>onAction()</tt> method invoked at: " + now;
+                // Return a partial containing the message
+                addModel("msg", msg);
+                return true;
+            }
+        });
+    }
+}

Modified: click/trunk/click/examples/webapp/WEB-INF/menu.xml
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/webapp/WEB-INF/menu.xml?rev=965047&r1=965046&r2=965047&view=diff
==============================================================================
--- click/trunk/click/examples/webapp/WEB-INF/menu.xml (original)
+++ click/trunk/click/examples/webapp/WEB-INF/menu.xml Sat Jul 17 09:54:28 2010
@@ -143,15 +143,16 @@
   <menu label="Ajax" path="#" imageSrc="/assets/images/lightning.png">
     <menu label="Ajax Demo - Behavior" path="ajax/ajax-behavior.htm"/>
     <menu label="Ajax Demo - Page Action" path="ajax/page-action.htm"/>
+    <menu label="Ajax Fallback Demo" path="ajax/ajax-fallback.htm"/>
     <menu separator="true"/>
-    <menu label="JavaScript AJAX Demo" path="ajax/compare/javascript-ajax-demo.htm"/>
-    <menu label="jQuery AJAX Demo" path="ajax/compare/jquery-ajax-demo.htm"/>
-    <menu label="Prototype AJAX Demo" path="ajax/compare/prototype-ajax-demo.htm"/>
+    <menu label="JavaScript Ajax Demo" path="ajax/compare/javascript-ajax-demo.htm"/>
+    <menu label="jQuery Ajax Demo" path="ajax/compare/jquery-ajax-demo.htm"/>
+    <menu label="Prototype Ajax Demo" path="ajax/compare/prototype-ajax-demo.htm"/>
     <menu separator="true"/>
-    <menu label="Table AJAX Demo" path="ajax/table/table-ajax.htm"/>
+    <menu label="Table Ajax Demo" path="ajax/table/table-ajax.htm"/>
     <menu separator="true"/>
-    <menu label="Simple Form AJAX Demo" path="ajax/form/simple-form-ajax.htm"/>
-    <menu label="Advanced Form AJAX Demo" path="ajax/form/advanced-form-ajax.htm"/>
+    <menu label="Simple Form Ajax Demo" path="ajax/form/simple-form-ajax.htm"/>
+    <menu label="Advanced Form Ajax Demo" path="ajax/form/advanced-form-ajax.htm"/>
     <menu separator="true"/>
     <menu label="XML response" path="ajax/content/xml-response.htm"/>
     <menu label="JSON response" path="ajax/content/json-response.htm"/>

Added: click/trunk/click/examples/webapp/ajax/ajax-fallback.htm
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/webapp/ajax/ajax-fallback.htm?rev=965047&view=auto
==============================================================================
--- click/trunk/click/examples/webapp/ajax/ajax-fallback.htm (added)
+++ click/trunk/click/examples/webapp/ajax/ajax-fallback.htm Sat Jul 17 09:54:28 2010
@@ -0,0 +1,71 @@
+<!--
+#* Licensed to the Apache Software Foundation (ASF) under one
+   or more contributor license agreements.  See the NOTICE file
+   distributed with this work for additional information
+   regarding copyright ownership.  The ASF licenses this file
+   to you under the Apache License, Version 2.0 (the
+   "License"); you may not use this file except in compliance
+   with the License.  You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing,
+   software distributed under the License is distributed on an
+   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+   KIND, either express or implied.  See the License for the
+   specific language governing permissions and limitations
+   under the License.*#
+-->
+
+This example demonstrates how to support both an Ajax and non-Ajax <tt>action</tt>
+event. If JavaScript is enabled an Ajax request is made, if JavaScript is disabled
+a normal HTTP request is made. On the server an <tt>AjaxBehavior</tt> handles the
+Ajax request while an <tt>ActionListener</tt> handles the non-Ajax event.
+
+<p/>
+
+Click $link to call the server (disable the browser JavaScript to see the fallback
+behavior in action)
+
+<div id="result">
+    <!-- The AJAX and non-AJAX response will be displayed here -->
+    #if($msg)
+      <p class="infoMsg">
+        $msg
+      <p/>
+    #end
+</div>
+
+<script type="text/javascript" src="$context/assets/js/jquery-1.4.2.js"></script>
+
+<script type="text/javascript">
+    // This example uses jQuery for making Ajax requests:
+    // http://api.jquery.com/jQuery.get/
+    // http://api.jquery.com/jQuery.ajax/
+
+    // Register a function that is invoked as soon as the DOM is loaded
+    jQuery(document).ready(function() {
+
+        // Register a 'click' handler that makes an Ajax request
+        jQuery("#link-id").click(function(event){
+            // Make ajax request
+            makeRequest();
+
+            // Prevent the default browser behavior of navigating to the link
+            return false;
+        })
+    })
+
+    function makeRequest() {
+        var link = jQuery('#link-id');
+        var extraData = link.attr('id') + '=1';
+        var url = link.attr('href');
+        jQuery.get(url, extraData, function(data) {
+            // 'data' is the response received from the server
+
+            // We select the div element with the ID 'result' and set its
+            // content to the server response
+            jQuery("#result").html("<p class='infoMsg'>" + data + "</p>");
+        });
+    }
+</script>