[Checkins] SVN: z3c.formjs/trunk/src/z3c/formjs/js Fixed jsvalidator tests.

Stephan Richter srichter at cosmos.phy.tufts.edu
Sat Jul 7 10:46:24 EDT 2007


Log message for revision 77571:
  Fixed jsvalidator tests.
  
  By writing another tests for the JS Handlers, it turned out that it did 
  not work correcly. This is now fixed.
  
  

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

-=-
Modified: z3c.formjs/trunk/src/z3c/formjs/jsaction.py
===================================================================
--- z3c.formjs/trunk/src/z3c/formjs/jsaction.py	2007-07-07 12:13:15 UTC (rev 77570)
+++ z3c.formjs/trunk/src/z3c/formjs/jsaction.py	2007-07-07 14:46:24 UTC (rev 77571)
@@ -100,7 +100,7 @@
             handler = self._registry.lookup(
                 (fieldProvided, eventProvided), interfaces.IJSEventHandler)
             if handler:
-                handlers += (handler,)
+                handlers += ((event, handler),)
         return handlers
 
     def copy(self):
@@ -128,8 +128,7 @@
 class JSHandler(object):
     zope.interface.implements(interfaces.IJSEventHandler)
 
-    def __init__(self, event, func):
-        self.event = event
+    def __init__(self, func):
         self.func = func
 
     def __call__(self, event, selector, request):
@@ -146,7 +145,7 @@
     if IField.providedBy(field):
         field = field.field
     def createHandler(func):
-        handler = JSHandler(event, func)
+        handler = JSHandler(func)
         frame = sys._getframe(1)
         f_locals = frame.f_locals
         handlers = f_locals.setdefault('jshandlers', JSHandlers())
@@ -159,8 +158,12 @@
 @zope.component.adapter(IAfterWidgetUpdateEvent)
 def createSubscriptionsForWidget(event):
     widget = event.widget
+    # Only react to widgets that have a field and know the form.
     if not (IFieldWidget.providedBy(widget) and IFormAware.providedBy(widget)):
         return
+    # We only have work to do, if there are JS Handlers in the form.
+    if not hasattr(widget.form, 'jshandlers'):
+        return
     # Step 1: Get the handler.
     handlers = widget.form.jshandlers.getHandlers(widget.field)
     # Step 2: Create a selector.
@@ -172,5 +175,5 @@
         zope.interface.alsoProvides(
             widget.form, interfaces.IHaveJSSubscriptions)
     # Step 4: Add the subscription to the form:
-    for handler in handlers:
-        widget.form.jsSubscriptions.subscribe(handler.event, selector, handler)
+    for event, handler in handlers:
+        widget.form.jsSubscriptions.subscribe(event, selector, handler)

Modified: z3c.formjs/trunk/src/z3c/formjs/jsaction.txt
===================================================================
--- z3c.formjs/trunk/src/z3c/formjs/jsaction.txt	2007-07-07 12:13:15 UTC (rev 77570)
+++ z3c.formjs/trunk/src/z3c/formjs/jsaction.txt	2007-07-07 14:46:24 UTC (rev 77571)
@@ -103,8 +103,6 @@
 
   >>> from z3c.form import button, form
 
-  # XXX: the handler should also receive the event
-
   >>> class Form(form.Form):
   ...     buttons = button.Buttons(IButtons)
   ...
@@ -310,7 +308,72 @@
        selector=<WidgetSelector "form-buttons-hello">,
        handler=<JSHandler <function showDoubleHelloWorldMessage ...>>>]
 
+Let's now look at a case where one handler is registered for all buttons and
+events, and another that overrides the click of hello to something else:
 
+  >>> from z3c.form.interfaces import IButton
+  >>> class Form(form.Form):
+  ...     buttons = button.Buttons(IButtons)
+  ...
+  ...     @jsaction.handler(IButton, interfaces.IJSEvent)
+  ...     def showHelloWorldMessage(self, event, selector):
+  ...         return '''alert("The event '%s' occured.");''' %event.name
+  ...
+  ...     @jsaction.handler(buttons['hello'], event=jsevent.CLICK)
+  ...     def showDoubleHelloWorldMessage(self, event, selector):
+  ...         return 'alert("Hello World clicked!");'
+
+  >>> demoform = Form(None, request)
+  >>> demoform.update()
+
+Let's now have a look at the rendered subscriptions:
+
+  >>> renderer = zope.component.getMultiAdapter(
+  ...     (demoform.jsSubscriptions, request), interfaces.IRenderer)
+  >>> renderer.update()
+  >>> print renderer.render()
+  $(document).ready(function(){
+    $("#...-hello").bind("click", function(){alert("Hello World clicked!");});
+    $("#...-hello").bind("dblclick", function(){alert("The ...");});
+    $("#...-hello").bind("change", function(){alert("The ...");});
+    $("#...-hello").bind("load", function(){alert("The ...");});
+    $("#...-hello").bind("blur", function(){alert("The ...");});
+    $("#...-hello").bind("focus", function(){alert("The ...");});
+    $("#...-hello").bind("keydown", function(){alert("The ...");});
+    $("#...-hello").bind("keyup", function(){alert("The ...");});
+    $("#...-hello").bind("mousedown", function(){alert("The ...");});
+    $("#...-hello").bind("mousemove", function(){alert("The ...");});
+    $("#...-hello").bind("mouseout", function(){alert("The ...");});
+    $("#...-hello").bind("mouseover", function(){alert("The ...");});
+    $("#...-hello").bind("mouseup", function(){alert("The ...");});
+    $("#...-hello").bind("resize", function(){alert("The ...");});
+    $("#...-hello").bind("select", function(){alert("The ...");});
+    $("#...-hello").bind("submit", function(){alert("The ...");});
+    $("#...-dblhello").bind("click", function(){alert("The ...");});
+    $("#...-dblhello").bind("dblclick", function(){alert("The ...");});
+    $("#...-dblhello").bind("change", function(){alert("The ...");});
+    $("#...-dblhello").bind("load", function(){alert("The ...");});
+    $("#...-dblhello").bind("blur", function(){alert("The ...");});
+    $("#...-dblhello").bind("focus", function(){alert("The ...");});
+    $("#...-dblhello").bind("keydown", function(){alert("The ...");});
+    $("#...-dblhello").bind("keyup", function(){alert("The ...");});
+    $("#...-dblhello").bind("mousedown", function(){alert("The ...");});
+    $("#...-dblhello").bind("mousemove", function(){alert("The ...");});
+    $("#...-dblhello").bind("mouseout", function(){alert("The ...");});
+    $("#...-dblhello").bind("mouseover", function(){alert("The ...");});
+    $("#...-dblhello").bind("mouseup", function(){alert("The ...");});
+    $("#...-dblhello").bind("resize", function(){alert("The ...");});
+    $("#...-dblhello").bind("select", function(){alert("The ...");});
+    $("#...-dblhello").bind("submit", function(){alert("The ...");});
+  })
+
+While this output might seem excessive, it demonstrates that the generic
+``IJSEvent`` subscription truly causes a subscription to all events. Further,
+a more specific directive takes precendence over the more generic one. This is
+due to the built-in adapter registry of the ``JSHandlers`` class.
+
+
+
 Submit and Javascript Buttons Together
 --------------------------------------
 

Modified: z3c.formjs/trunk/src/z3c/formjs/jsvalidator.txt
===================================================================
--- z3c.formjs/trunk/src/z3c/formjs/jsvalidator.txt	2007-07-07 12:13:15 UTC (rev 77570)
+++ z3c.formjs/trunk/src/z3c/formjs/jsvalidator.txt	2007-07-07 14:46:24 UTC (rev 77571)
@@ -31,7 +31,7 @@
     ...     fields = field.Fields(IAddress)
     ...
     ...     @jsaction.handler(interfaces.IField, event=jsevent.CHANGE)
-    ...     def fieldValidator(self, selector):
+    ...     def fieldValidator(self, event, selector):
     ...         return self.ValidationScript(self, selector.widget).render()
 
     >>> from z3c.form.testing import TestRequest



More information about the Checkins mailing list