changeset 1463:607657eb2865

Properly handle form textboxes that have sources
author Adam Chlipala <adam@chlipala.net>
date Sun, 29 May 2011 12:44:31 -0400
parents c12ceb891350
children 969b90b1f2f9
files lib/js/urweb.js src/monoize.sml tests/tsource.ur tests/tsource.urs
diffstat 4 files changed, 38 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/lib/js/urweb.js	Sun May 29 12:18:59 2011 -0400
+++ b/lib/js/urweb.js	Sun May 29 12:44:31 2011 -0400
@@ -461,7 +461,8 @@
     populate(x);
 }
 
-function input(x, s, recreate, type) {
+function input(x, s, recreate, type, name) {
+    if (name) x.name = name;
     if (type) x.type = type;
     x.dead = false;
     x.signal = ss(s);
@@ -473,9 +474,9 @@
     return x;
 }
 
-function inp(s) {
+function inp(s, name) {
     var x = input(document.createElement("input"), s,
-                  function(x) { return function(v) { if (x.value != v) x.value = v; }; });
+                  function(x) { return function(v) { if (x.value != v) x.value = v; }; }, null, name);
     x.value = s.data;
     x.onkeyup = function() { sv(s, x.value) };
 
--- a/src/monoize.sml	Sun May 29 12:18:59 2011 -0400
+++ b/src/monoize.sml	Sun May 29 12:44:31 2011 -0400
@@ -2737,8 +2737,8 @@
                (L.EFfi ("Basis", "sql_nfunc"), _),
                _), _),
               _), _),
-             _), _),
-            _) =>
+             _), _), 
+           _) =>
             let
                 val s = (L'.TFfi ("Basis", "string"), loc)
                 fun sc s = (L'.EPrim (Prim.String s), loc)
@@ -3258,7 +3258,9 @@
                              | SOME (_, src, _) =>
                                (strcat [str "<script type=\"text/javascript\">inp(exec(",
                                         (L'.EJavaScript (L'.Script, src), loc),
-                                        str "))</script>"],
+                                        str "), \"",
+                                        str name,
+                                        str "\")</script>"],
                                 fm))
                         | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
                                 raise Fail "No name passed to textbox tag"))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/tsource.ur	Sun May 29 12:44:31 2011 -0400
@@ -0,0 +1,28 @@
+fun doSubmit r =
+  return <xml>Done {[readError r.Amount1 * readError r.Amount2 * 2.0]}</xml>
+
+fun main () =
+  amount1S <- source "1";
+  amount2S <- source "1";
+  return <xml>  <body>
+   <form>
+      <table>
+        <tr><td>Amount1:</td><td><textbox{#Amount1}
+source={amount1S}/></td></tr>
+        <tr><td>Amount2:</td><td><textbox{#Amount2}
+source={amount2S}/></td></tr>
+        <tr><td>Total:</td><td><dyn signal={showTotal amount1S
+amount2S}/></td></tr>
+      </table>
+      <submit value="Buy" action={doSubmit}/>
+    </form>
+  </body>
+</xml>
+
+and showTotal amount1S amount2S =
+  a1 <- signal amount1S;
+  a2 <- signal amount2S;
+  return (case ((read a1), (read a2)) of
+           (None, _) => <xml></xml>
+         | (_, None) => <xml></xml>
+         | (Some a, Some b) => <xml>{[a * b * 2.0]}</xml>)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/tsource.urs	Sun May 29 12:44:31 2011 -0400
@@ -0,0 +1,1 @@
+val main : unit -> transaction page