[Checkins] SVN: z3c.formjs/trunk/src/z3c/formjs/jsaction. added buttonAndHandler decorator just like the one in z3c.form, but for jsbuttons.

Paul Carduner paulcarduner at gmail.com
Sun May 4 19:52:27 EDT 2008


Log message for revision 86431:
  added buttonAndHandler decorator just like the one in z3c.form, but for jsbuttons.

Changed:
  U   z3c.formjs/trunk/src/z3c/formjs/jsaction.py
  U   z3c.formjs/trunk/src/z3c/formjs/jsaction.txt

-=-
Modified: z3c.formjs/trunk/src/z3c/formjs/jsaction.py
===================================================================
--- z3c.formjs/trunk/src/z3c/formjs/jsaction.py	2008-05-04 23:43:00 UTC (rev 86430)
+++ z3c.formjs/trunk/src/z3c/formjs/jsaction.py	2008-05-04 23:52:27 UTC (rev 86431)
@@ -165,7 +165,23 @@
         return func
     return createHandler
 
+def buttonAndHandler(title, **kwargs):
+    # Add the title to button constructor keyword arguments
+    kwargs['title'] = title
+    # Extract directly provided interfaces:
+    provides = kwargs.pop('provides', ())
+    # Create button and add it to the button manager
+    jsButton = JSButton(**kwargs)
+    zope.interface.alsoProvides(jsButton, provides)
+    frame = sys._getframe(1)
+    f_locals = frame.f_locals
+    buttons = f_locals.setdefault('buttons', button.Buttons())
+    f_locals['buttons'] += button.Buttons(jsButton)
+    # Return the handler decorator
+    return handler(button)
 
+
+
 @zope.interface.implementer(zope.interface.Interface)
 @zope.component.adapter(IAfterWidgetUpdateEvent)
 def createSubscriptionsForWidget(event):

Modified: z3c.formjs/trunk/src/z3c/formjs/jsaction.txt
===================================================================
--- z3c.formjs/trunk/src/z3c/formjs/jsaction.txt	2008-05-04 23:43:00 UTC (rev 86430)
+++ z3c.formjs/trunk/src/z3c/formjs/jsaction.txt	2008-05-04 23:52:27 UTC (rev 86431)
@@ -15,7 +15,7 @@
 ------------------
 
 Before we can write a form that uses Javascript buttons, we have to define
-them first. One common way to define buttons in ``z3c.form`` is to write an
+them first. One common way to define buttons in ``z3c.form`` is to write a
 schema describing them; so let's do that now:
 
   >>> import zope.interface
@@ -111,10 +111,12 @@
 Forms with Javascript Buttons
 -----------------------------
 
-The next step is create the form. Luckily we do not need any fields to render
-a form. Also, instead of using usual ``z3c.form.button.handler()`` function,
-we now have a special handler decorator that connects a button to a Javascript
-event. The output of the handler itself is a string that is used as the
+The next step is create the form. Luckily we do not need any fields to
+render a form. Also, instead of using usual
+``z3c.form.button.handler()`` function, we now have a special handler
+decorator that connects a button to a Javascript event, along with an
+additional decorator that creates the button at the same time. The
+output of the handler itself is a string that is used as the
 Javascript script that is executed.
 
   >>> from z3c.form import button, form
@@ -129,7 +131,13 @@
   ...     @jsaction.handler(buttons['dblhello'], event=jsevent.DBLCLICK)
   ...     def showDoubleHelloWorldMessage(self, event, selector):
   ...         return 'alert("%s");' % selector.widget.title
+  ...
+  ...     @jsaction.buttonAndHandler(u"Click Me")
+  ...     def handleClickMe(self, event, selector):
+  ...         return 'alert("You clicked the Click Me button.");'
 
+
+
 The ``handler()`` decorator takes two arguments, the button (acting as the DOM
 element selector) and the event to which to bind the action. By default the
 event is ``jsevent.CLICK``.
@@ -165,7 +173,7 @@
 actions:
 
   >>> actions.keys()
-  ['hello', 'dblhello']
+  ['hello', 'dblhello', '436c69636b204d65']
   >>> actions['hello']
   <JSButtonAction 'form.buttons.hello' u'Hello World!'>
 
@@ -288,6 +296,11 @@
                name="form.buttons.dblhello" class="button-widget jsbutton-field"
                value="Double Hello World!" />
       </div>
+      <div class="action">
+        <input type="button" id="form-buttons-436c69636b204d65"
+         name="form.buttons.436c69636b204d65"
+         class="button-widget jsbutton-field" value="Click Me" />
+      </div>
     </body>
   </html>
 



More information about the Checkins mailing list