[Zope] TAL and Javascript

J Cameron Cooper zope-l at jcameroncooper.com
Wed Jun 29 16:03:50 EDT 2005


Rob Boyd wrote:
> Thanks to all the responders. It gave me some ideas, but alas no luck.
> 
> To clarify, I am not trying to do everything in one request. Request
> one generates the page, a user event (selecting an option from a form)
> fires another request via javascript.

I do not see it creating another request. I see a DOM event. DOM events 
do not touch the server.

> like:
> <script tal:content="python:'function getFormats() {
>     var widget = document.getElementById('orgs');
>     var chosen_org = widget.options[widget.selectedIndex].value;
>     var formats = %s;
>     // build selection 2 options from formats
>     ' % here.getDataFormats(chosen_org)" />
> 
> And then a user event (onChange) calls getFormats.
> 
> Even with generating the script with a Python script or DTML, the
> problem remains (for me at least) to get a value from the DOM passed as
> an argument to a Python script that queries my database.

Again, as I understand you, this is impossible.

Zope has no access to the DOM. Only to request parameters. This simply 
cannot work. ZPT can only insert values during page rendering, and the 
value for 'chosen_org' is decided after the page is rendered and in a 
browser. Unless you can invent a special time-traveling Javascript, this 
cannot work.

What would work is::

  <script tal:content="python:'function getFormats() {
      var widget = document.getElementById('orgs');
      var chosen_org = widget.options[widget.selectedIndex].value;
      var formats = xmlrpcserver.getDataFormats(chosen_org);'" />

This would actually create another request to Zope, running a script 
with you're dynamically generated value. Note: I don't know how xml-rpc 
is done in JavaScript, so I just made something up. Presumably 
'xmlrpcserver' is a handle on your Zope server. Also note that every 
time someone fires off this method, it's a request to the server.

(Also note: it doesn't have to be XML-RPC, just some way of asking the 
server. There are various methods used for this.)

You might also make the widget that you chose from be part of a form, 
and when a value is chosen, the form is submitted. This will get the 
'chosen_org' in a request, and your template can then render your second 
select list with the values from::

   'here.getDataFormats(request.chosen_org)'

This is the common 'old-school' way of doing this.

		--jcc
-- 
"Building Websites with Plone"
http://plonebook.packtpub.com/

Enfold Systems, LLC
http://www.enfoldsystems.com


More information about the Zope mailing list