click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r782724 - in /incubator/click/trunk/click/examples: src/org/apache/click/examples/page/ajax/AjaxSelect.java src/org/apache/click/examples/page/pageflow/StartPage.java webapp/ajax/ajax-customer.htm webapp/ajax/ajax-select.js
Date Mon, 08 Jun 2009 18:50:21 GMT
Author: sabob
Date: Mon Jun  8 18:50:21 2009
New Revision: 782724

URL: http://svn.apache.org/viewvc?rev=782724&view=rev
Log:
replaced Rico with Prototype for Ajax select demo

Added:
    incubator/click/trunk/click/examples/webapp/ajax/ajax-select.js
Modified:
    incubator/click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxSelect.java
    incubator/click/trunk/click/examples/src/org/apache/click/examples/page/pageflow/StartPage.java
    incubator/click/trunk/click/examples/webapp/ajax/ajax-customer.htm

Modified: incubator/click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxSelect.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxSelect.java?rev=782724&r1=782723&r2=782724&view=diff
==============================================================================
--- incubator/click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxSelect.java
(original)
+++ incubator/click/trunk/click/examples/src/org/apache/click/examples/page/ajax/AjaxSelect.java
Mon Jun  8 18:50:21 2009
@@ -18,13 +18,16 @@
  */
 package org.apache.click.examples.page.ajax;
 
-import java.util.Iterator;
+import java.util.HashMap;
 import java.util.List;
 
+import java.util.Map;
 import javax.annotation.Resource;
 
 import org.apache.click.control.Option;
 import org.apache.click.control.Select;
+import org.apache.click.element.JsImport;
+import org.apache.click.element.JsScript;
 import org.apache.click.examples.domain.Customer;
 import org.apache.click.examples.page.BorderPage;
 import org.apache.click.examples.service.CustomerService;
@@ -38,8 +41,6 @@
 @Component
 public class AjaxSelect extends BorderPage {
 
-    public String jsInclude = "ajax/ajax-select-include.htm";
-
     public Select customerSelect = new Select("customerSelect");
 
     @Resource(name="customerService")
@@ -49,8 +50,6 @@
     public void onInit() {
         super.onInit();
 
-        customerSelect.setAttribute("onchange", "onCustomerChange(this);");
-
         List<Customer> customerList = customerService.getCustomersSortedByName(8);
         for (Customer customer : customerList) {
             customerSelect.add(new Option(customer.getId(), customer.getName()));
@@ -59,4 +58,33 @@
         customerSelect.setSize(customerList.size());
     }
 
+    public List getHeadElements() {
+        if (headElements == null) {
+            headElements = super.getHeadElements();
+
+            // Include the prototype.js library which is made available under
+            // the web folder "/click/prototype/"
+            headElements.add(new JsImport("/click/prototype/prototype.js"));
+
+            // Create a model to pass to the Page JavaScript template. The
+            // template recognizes three Velocity variables:
+            // $context, $selector and $target
+            Map model = new HashMap();
+
+            // Add the application context path -> "/click-examples"
+            model.put("context", getContext().getRequest().getContextPath());
+
+            // Add a CSS selector, in this case the customerSelect ID attribute
+            model.put("selector", customerSelect.getId());
+
+            // Add the ID of a target element in the Page template to replace
+            // with new data, in this example the target is 'customerDetails'
+            model.put("target", "customerDetails");
+
+            // Include the Page associated JavaScript template
+            headElements.add(new JsScript("/ajax/ajax-select.js", model));
+        }
+
+        return headElements;
+    }
 }

Modified: incubator/click/trunk/click/examples/src/org/apache/click/examples/page/pageflow/StartPage.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/src/org/apache/click/examples/page/pageflow/StartPage.java?rev=782724&r1=782723&r2=782724&view=diff
==============================================================================
--- incubator/click/trunk/click/examples/src/org/apache/click/examples/page/pageflow/StartPage.java
(original)
+++ incubator/click/trunk/click/examples/src/org/apache/click/examples/page/pageflow/StartPage.java
Mon Jun  8 18:50:21 2009
@@ -18,8 +18,10 @@
  */
 package org.apache.click.examples.page.pageflow;
 
+import java.util.HashMap;
 import java.util.List;
 
+import java.util.Map;
 import javax.annotation.Resource;
 
 import org.apache.click.control.Form;
@@ -27,6 +29,8 @@
 import org.apache.click.control.Select;
 import org.apache.click.control.Submit;
 import org.apache.click.control.TextArea;
+import org.apache.click.element.JsImport;
+import org.apache.click.element.JsScript;
 import org.apache.click.examples.control.InvestmentSelect;
 import org.apache.click.examples.domain.CourseBooking;
 import org.apache.click.examples.domain.Customer;
@@ -44,10 +48,7 @@
 @Component
 public class StartPage extends BorderPage {
 
-    public String jsInclude = "ajax/ajax-select-include.htm";
-    public String bodyOnload = "registerAjax();";
     public Form form = new Form();
-
     private Select customerSelect;
     private DateField dateField;
     private Select courseSelect;
@@ -65,7 +66,6 @@
 
         customerSelect = new Select("Customer");
         customerSelect.setRequired(true);
-        customerSelect.setAttribute("onchange", "onCustomerChange(this);");
         form.add(customerSelect);
 
         dateField = new DateField("Booking Date");
@@ -138,6 +138,41 @@
         return true;
     }
 
+    /**
+     * Return the Page JavaScript resources.
+     *
+     * @see org.apache.click.Page#getHeadElements()
+     */
+    public List getHeadElements() {
+        if (headElements == null) {
+            headElements = super.getHeadElements();
+
+            // Include the prototype.js library which is made available under
+            // the web folder "/click/prototype/"
+            headElements.add(new JsImport("/click/prototype/prototype.js"));
+
+            // Create a model to pass to the Page JavaScript template. The
+            // template recognizes three Velocity variables:
+            // $context, $selector and $target
+            Map model = new HashMap();
+
+            // Add the application context path -> "/click-examples"
+            model.put("context", getContext().getRequest().getContextPath());
+
+            // Add a CSS selector, in this case the customerSelect ID attribute
+            model.put("selector", customerSelect.getId());
+
+            // Add the ID of a target element in the Page template to replace
+            // with new data, in this example the target is 'customerDetails'
+            model.put("target", "customerDetails");
+
+            // Include the ajax-select.js template
+            headElements.add(new JsScript("/ajax/ajax-select.js", model));
+        }
+
+        return headElements;
+    }
+
     // --------------------------------------------------------- Public Methods
 
     public void setCourseBooking(CourseBooking courseBooking) {

Modified: incubator/click/trunk/click/examples/webapp/ajax/ajax-customer.htm
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/webapp/ajax/ajax-customer.htm?rev=782724&r1=782723&r2=782724&view=diff
==============================================================================
--- incubator/click/trunk/click/examples/webapp/ajax/ajax-customer.htm (original)
+++ incubator/click/trunk/click/examples/webapp/ajax/ajax-customer.htm Mon Jun  8 18:50:21
2009
@@ -1,5 +1,3 @@
-<?xml version="1.0" encoding="UTF-8"?>
-
 <!--
 #* Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
@@ -19,10 +17,8 @@
    under the License.*#
 -->
 
-<ajax-response>
-<response type="element" id="customerDetails">
-#if ($customer) 
 <table border="0" cellspacing="2" cellpadding="2">
+#if ($customer)
  <tr>
   <td><b>Name</b></td>
   <td>$format.string($customer.name)</td>
@@ -47,7 +43,5 @@
   <td><b>Date Joined</b></td>
   <td>$format.date($customer.dateJoined, "dd MMM yyyy")</td>
  </tr>
-</table>
 #end
-</response>
-</ajax-response>
\ No newline at end of file
+</table>

Added: incubator/click/trunk/click/examples/webapp/ajax/ajax-select.js
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/webapp/ajax/ajax-select.js?rev=782724&view=auto
==============================================================================
--- incubator/click/trunk/click/examples/webapp/ajax/ajax-select.js (added)
+++ incubator/click/trunk/click/examples/webapp/ajax/ajax-select.js Mon Jun  8 18:50:21 2009
@@ -0,0 +1,47 @@
+// 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.
+
+/**
+ * The ajax-select Page JavaScript.
+ */
+
+// Wait until browser DOM is loaded
+document.observe("dom:loaded", function() {
+
+    // Observe the selected element's "change" event, which triggers the given function
+    $('$selector').observe('change', function(event){
+        var select = Event.element(event);
+        onCustomerChange(select);
+    });
+
+    // This function uses an Ajax request to retrieve customer details that matches
+    // the ID value of a select element
+    function onCustomerChange(select) {
+        // The Ajax url points to the AjaxCustomer Click Page
+        new Ajax.Request('${context}/ajax/ajax-customer.htm', {
+            method: 'get',
+            parameters: {customerId : select.value},
+            onSuccess: function(transport){
+                // On a successful request, update the given target with the server response
+                $('$target').update(transport.responseText);
+            },
+            onFailure: function(){
+                alert('An error occurred.')
+            }
+        });
+    }
+});



Mime
View raw message