[Checkins] SVN: z3c.formjs/trunk/src/z3c/formjs/ make it possible
to get a specific subscription from the subscription manager.
Paul Carduner
paulcarduner at gmail.com
Tue Aug 14 18:43:17 EDT 2007
Log message for revision 78824:
make it possible to get a specific subscription from the subscription manager.
Changed:
U z3c.formjs/trunk/src/z3c/formjs/interfaces.py
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/jsevent.py
U z3c.formjs/trunk/src/z3c/formjs/jsevent.txt
-=-
Modified: z3c.formjs/trunk/src/z3c/formjs/interfaces.py
===================================================================
--- z3c.formjs/trunk/src/z3c/formjs/interfaces.py 2007-08-14 22:21:54 UTC (rev 78823)
+++ z3c.formjs/trunk/src/z3c/formjs/interfaces.py 2007-08-14 22:43:16 UTC (rev 78824)
@@ -86,7 +86,10 @@
def __iter__():
"""Return an iterator of all subscriptions."""
+ def __getitem__(name):
+ """Return all the subscriptions for the handler with the given name."""
+
class IRenderer(zope.interface.Interface):
"""Render a component in the intended output format."""
@@ -173,6 +176,11 @@
class IJSEventHandler(zope.interface.Interface):
"""An action handler of Javascript events for fields and buttons."""
+ __name__ = zope.schema.ASCIILine(
+ title=u"Name",
+ description=u"The name of the handler. Like a function's name",
+ required=True)
+
def __call__(event, selector, request):
"""Call the handler.
@@ -299,4 +307,3 @@
class IFormTraverser(zope.interface.Interface):
"""Marker interface for forms that can be traversed by the @@ajax
view."""
-
Modified: z3c.formjs/trunk/src/z3c/formjs/jsaction.py
===================================================================
--- z3c.formjs/trunk/src/z3c/formjs/jsaction.py 2007-08-14 22:21:54 UTC (rev 78823)
+++ z3c.formjs/trunk/src/z3c/formjs/jsaction.py 2007-08-14 22:43:16 UTC (rev 78824)
@@ -135,6 +135,13 @@
def __init__(self, func):
self.func = func
+ # XXX: should this ever be passed a string?
+ # it is passed a string in unit tests, but I
+ # think we may want to make that an invalid operation.
+ if type(func) == str:
+ self.__name__ = func
+ else:
+ self.__name__ = func.__name__
def __call__(self, event, selector, request):
return self.func(selector.widget.form, event, selector)
Modified: z3c.formjs/trunk/src/z3c/formjs/jsaction.txt
===================================================================
--- z3c.formjs/trunk/src/z3c/formjs/jsaction.txt 2007-08-14 22:21:54 UTC (rev 78823)
+++ z3c.formjs/trunk/src/z3c/formjs/jsaction.txt 2007-08-14 22:43:16 UTC (rev 78824)
@@ -357,7 +357,6 @@
>>> 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 ...");});
@@ -389,6 +388,7 @@
$("#...-dblhello").bind("resize", function(){alert("The ...");});
$("#...-dblhello").bind("select", function(){alert("The ...");});
$("#...-dblhello").bind("submit", function(){alert("The ...");});
+ $("#...-hello").bind("click", function(){alert("Hello World clicked!");});
})
While this output might seem excessive, it demonstrates that the generic
@@ -520,6 +520,12 @@
... pass
>>> handler = jsaction.JSHandler(doSomething)
+The only special thing about the handler is that it has the same name
+as the function.
+
+ >>> handler.__name__
+ 'doSomething'
+
and a field/button:
>>> button1 = jsaction.JSButton(name='button1', title=u'Button 1')
Modified: z3c.formjs/trunk/src/z3c/formjs/jsevent.py
===================================================================
--- z3c.formjs/trunk/src/z3c/formjs/jsevent.py 2007-08-14 22:21:54 UTC (rev 78823)
+++ z3c.formjs/trunk/src/z3c/formjs/jsevent.py 2007-08-14 22:43:16 UTC (rev 78824)
@@ -94,17 +94,25 @@
zope.interface.implements(interfaces.IJSSubscriptions)
def __init__(self):
- self._subscriptions = []
+ self._subscriptions = {}
def subscribe(self, event, selector, handler):
subscription = JSSubscription(event, selector, handler)
- self._subscriptions.append(subscription)
+ name = handler.__name__
+ subscriptions = self._subscriptions.get(name, [])
+ subscriptions.append(subscription)
+ self._subscriptions[name] = subscriptions
return subscription
def __iter__(self):
- return iter(self._subscriptions)
+ for subscriptions in self._subscriptions.values():
+ for subscription in subscriptions:
+ yield subscription
+ def __getitem__(self, name):
+ return self._subscriptions[name]
+
def subscribe(selector, event=CLICK):
"""A decorator for defining a javascript event handler."""
def createSubscription(func):
Modified: z3c.formjs/trunk/src/z3c/formjs/jsevent.txt
===================================================================
--- z3c.formjs/trunk/src/z3c/formjs/jsevent.txt 2007-08-14 22:21:54 UTC (rev 78823)
+++ z3c.formjs/trunk/src/z3c/formjs/jsevent.txt 2007-08-14 22:43:16 UTC (rev 78824)
@@ -62,6 +62,13 @@
selector=<IdSelector "message">,
handler=<function showHelloWorldAlert at ...>>]
+We can also get a specific subscription from the manager.
+
+ >>> manager['showHelloWorldAlert']
+ [<JSSubscription event=<JSEvent "click">,
+ selector=<IdSelector "message">,
+ handler=<function showHelloWorldAlert at ...>>]
+
So now, how does this get rendered into Javascript code? Since this package
strictly separates definition from rendering, a renderer will be responsible
to produce the output.
More information about the Checkins
mailing list