Author: mrdon
Date: Sun Mar 20 15:13:18 2005
New Revision: 158371

URL: http://svn.apache.org/viewcvs?view=rev&rev=158371
Log:
 * Added continuation id in json return
 * Modified remote-example to have client method call modify server values
 * Refactored client library into object

Modified:
    struts/flow/trunk/src/java/org/apache/struts/flow/FlowAction.java
    struts/flow/trunk/src/remote-example/WEB-INF/numberguess.js
    struts/flow/trunk/src/remote-example/clientFlow.js
    struts/flow/trunk/src/remote-example/guess.jsp

Modified: struts/flow/trunk/src/java/org/apache/struts/flow/FlowAction.java
URL: 
http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/FlowAction.java?view=diff&r1=158370&r2=158371
==============================================================================
--- struts/flow/trunk/src/java/org/apache/struts/flow/FlowAction.java (original)
+++ struts/flow/trunk/src/java/org/apache/struts/flow/FlowAction.java Sun Mar 
20 15:13:18 2005
@@ -187,6 +187,7 @@
             }    
             
             if (isFlowCall) {
+                atts.put("contid", contid);
                 String json = new JSONSerializer().serialize(atts);
                 response.getWriter().write(json);
                 response.getWriter().flush();

Modified: struts/flow/trunk/src/remote-example/WEB-INF/numberguess.js
URL: 
http://svn.apache.org/viewcvs/struts/flow/trunk/src/remote-example/WEB-INF/numberguess.js?view=diff&r1=158370&r2=158371
==============================================================================
--- struts/flow/trunk/src/remote-example/WEB-INF/numberguess.js (original)
+++ struts/flow/trunk/src/remote-example/WEB-INF/numberguess.js Sun Mar 20 
15:13:18 2005
@@ -1,10 +1,11 @@
 var random;
+var guesses;
 
 function main() {
 
   random =  Math.round( Math.random() * 9 ) + 1;
   var hint = "No hint for you!"
-  var guesses = 0;
+  guesses = 0;
 
   while (true) {
 
@@ -39,5 +40,6 @@
 }
 
 function cheat() {
-    return {"secret":random};
+    guesses += 5;
+    return {"secret":random, "guesses":guesses};
 }

Modified: struts/flow/trunk/src/remote-example/clientFlow.js
URL: 
http://svn.apache.org/viewcvs/struts/flow/trunk/src/remote-example/clientFlow.js?view=diff&r1=158370&r2=158371
==============================================================================
--- struts/flow/trunk/src/remote-example/clientFlow.js (original)
+++ struts/flow/trunk/src/remote-example/clientFlow.js Sun Mar 20 15:13:18 2005
@@ -1,3 +1,24 @@
+function ClientFlow(url) {
+    this.method = "POST";
+    this.urlBase = url;
+}
+
+ClientFlow.prototype.call = function(func, contid, data) {
+    if (!data) data = [];
+    url = this.urlBase + "?contid="+contid+"&FlowCall="+func;
+    req = this.xmlHTTPRequestObject();
+    if (req) {
+      req.open (this.method, url, false);
+      json = this.stringify(data);
+      //alert("sending:"+json);
+      req.send (json);
+      //alert("response:"+req.responseText);
+      eval("ret = "+req.responseText);
+      return ret;
+    }
+    return false;  
+}
+
 /* XMLHTTP functions 0.2 */
 /* For all the Railsers out there */
 
@@ -8,38 +29,7 @@
 
 /* The licence is simple, use however you want, but leave attribution to any 
authors
    listed above, including yourself :-) */
-
-function flowCall(url, func, contid, data) {
-  method = "POST";
-  if (!data) data = null;
-  url = url + "?contid="+contid+"&FlowCall="+func;
-  req = xmlHTTPRequestObject();
-  if (req) {
-         req.open (method, url, false);
-      json = stringify(data);
-      //alert("sending:"+json);
-         req.send (json);
-      //alert("response:"+req.responseText);
-         eval("ret = "+req.responseText);
-      return ret;
-  }
-  return false;
-}
-
-/*function xmlHTTPAsyncRequest(url, method, data, callbackr) {
-  if (!method) method = "GET";
-  if (!data) data = null;
-  req = xmlHTTPRequestObject();
-  if (req) {
-       eval ('req.onreadystatechange = ' + callbackr + ';');
-       req.open (method, url, true);
-       req.send (data);
-       return req
-  }
-}
-*/
-
-function xmlHTTPRequestObject() {
+ClientFlow.prototype.xmlHTTPRequestObject = function() {
        var obj = false;
        var objectIDs = new Array(
                "Microsoft.XMLHTTP",
@@ -62,11 +52,11 @@
        return obj;
 }
 
-function isXMLHTTPRequestSupported() {
+ClientFlow.prototype.isXMLHTTPRequestSupported = function() {
        return xmlHTTPRequestObject != null;
 }
 
-function stringify(arg) {
+ClientFlow.prototype.stringify = function(arg) {
     var i, o, u, v;
 
     switch (typeof arg) {

Modified: struts/flow/trunk/src/remote-example/guess.jsp
URL: 
http://svn.apache.org/viewcvs/struts/flow/trunk/src/remote-example/guess.jsp?view=diff&r1=158370&r2=158371
==============================================================================
--- struts/flow/trunk/src/remote-example/guess.jsp (original)
+++ struts/flow/trunk/src/remote-example/guess.jsp Sun Mar 20 15:13:18 2005
@@ -4,16 +4,20 @@
   <title>Struts Flow number guessing game</title>
     <script type="text/javascript">
   <!--
-var contid = "<%=request.getAttribute("contid")%>";
+function init() {
+    this.contid = "<%=request.getAttribute("contid")%>";
+    this.client = new ClientFlow("guess.do");
+}   
 function cheat() {
-    result = flowCall("guess.do", "cheat", contid, []);
-    alert("The secret number is "+result.secret);
+    result = client.call("cheat", contid);
+    alert("The secret number is "+result.secret+". After applying a penalty, 
you have guessed "+result.guesses+" times");
+    contid = result.contid;
 }
     -->
   </script>
   <script type="text/javascript" src="clientFlow.js" />
 </head>
-<body>
+<body onload="init()">
 
   <h1>Guess the Number Between 1 and 10</h1>
   



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to