[Checkins] SVN: z3c.formjs/trunk/src/z3c/formjs/ Add function to render calls to jsfunctions.

Paul Carduner paulcarduner at gmail.com
Tue Aug 21 17:27:08 EDT 2007


Log message for revision 79100:
  Add function to render calls to jsfunctions.

Changed:
  U   z3c.formjs/trunk/src/z3c/formjs/interfaces.py
  U   z3c.formjs/trunk/src/z3c/formjs/jsfunction.py
  U   z3c.formjs/trunk/src/z3c/formjs/jsfunction.txt

-=-
Modified: z3c.formjs/trunk/src/z3c/formjs/interfaces.py
===================================================================
--- z3c.formjs/trunk/src/z3c/formjs/interfaces.py	2007-08-21 21:24:14 UTC (rev 79099)
+++ z3c.formjs/trunk/src/z3c/formjs/interfaces.py	2007-08-21 21:27:08 UTC (rev 79100)
@@ -117,7 +117,10 @@
     def render():
         """Render the content of the JS function."""
 
+    def call():
+        """Render a JavaScript call to the function."""
 
+
 class IJSFunctions(zope.interface.Interface):
     """A manager of Javascript functions."""
 

Modified: z3c.formjs/trunk/src/z3c/formjs/jsfunction.py
===================================================================
--- z3c.formjs/trunk/src/z3c/formjs/jsfunction.py	2007-08-21 21:24:14 UTC (rev 79099)
+++ z3c.formjs/trunk/src/z3c/formjs/jsfunction.py	2007-08-21 21:27:08 UTC (rev 79100)
@@ -49,6 +49,23 @@
     def render(self):
         return self.function(*[x for x in ['self'] + self.arguments])
 
+    def call(self, *args):
+        sargs = []
+        for arg in args:
+            argtype = type(arg)
+            if argtype is unicode:
+                sargs.append(repr(arg)[1:])
+            elif argtype is bool:
+                sargs.append(repr(arg).lower())
+            elif argtype in (int, float, str):
+                sargs.append(repr(arg))
+            else:
+                sargs.append(repr(str(arg)))
+        caller = self.name
+        if self.namespace:
+            caller = '%s.%s' % (self.namespace, self.name)
+        return '%s(%s);' % (caller, ','.join(sargs))
+
     def __repr__(self):
         return '<%s %s>' % (
             self.__class__.__name__, self.name)

Modified: z3c.formjs/trunk/src/z3c/formjs/jsfunction.txt
===================================================================
--- z3c.formjs/trunk/src/z3c/formjs/jsfunction.txt	2007-08-21 21:24:14 UTC (rev 79099)
+++ z3c.formjs/trunk/src/z3c/formjs/jsfunction.txt	2007-08-21 21:27:08 UTC (rev 79100)
@@ -113,7 +113,30 @@
 
 And that is realy everything that there is to it.
 
+Calling JSFunctions from Python
+-------------------------------
 
+We can also render a JavaScript call to a JSFunction from within
+python using the call method of the function.  We can even pass
+the call method the arguments to be rendered.
+
+  >>> View.show.call(u"Some Title")
+  "show('Some Title');"
+  >>> View.show.call("Some Title")
+  "show('Some Title');"
+  >>> View.show.call(3.0)
+  'show(3.0);'
+  >>> View.show.call(1)
+  'show(1);'
+  >>> View.show.call(True)
+  'show(true);'
+
+Unsupported data types are just rendered as strings.
+
+  >>> View.show.call(object())
+  "show('<object object at ...>');"
+
+
 -----------------
 
   >>> from z3c.formjs.tests import jsfunction



More information about the Checkins mailing list