[Zope] TAL and Javascript

2005-06-29 Thread Rob Boyd
I'm stuck this, and would appreciate help or pointers.

I have a form with 2 selection drop-downs. I want the user's choice of
select 1 to drive the options displayed in select 2. When the user
makes a selection in select 1, onChange calls a Javascript function
that should write select 2 options based on the result of a call to a
Python script, passing the select 1 choice as an argument to the Python
script.

But: I cannot get the Javascript var into the namespace that the TALES
expression knows about.

Example:

script
function makeDropDown() {
  var widget = document.getElementById('select1');
  var choice = widget.options[widget.selectedIndex].value;
  var data = [result of calling Python script 'foo(arg)' with
arg=choice]
  // create options for select 2
}
/script
[various html...]
select id=select1 onChange=makeDropDown()
[options...]
/select

I've tried with multiple scripts, where one has tal:content=... but
cannot figure out how the TALES expression can get at what my
javascript gets from an event. I haven't done much with JavaScript
inside Page Templates, so perhaps I'm going about this all wrong.

TIA,
Rob




__ 
Discover Yahoo! 
Stay in touch with email, IM, photo sharing and more. Check it out! 
http://discover.yahoo.com/stayintouch.html
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] TAL and Javascript

2005-06-29 Thread Peter Bengtsson
What I do when the javascript gets too hairy is to stick it in a DTML
Method and then do something like this::

script type=text/javascript tal:content=structure
here/the_script_dtml/script
or
script type=text/javascript tal:content=structure
python:here.the_script_dtml(here, request, parameterX='Y')/script

On 6/29/05, Rob Boyd [EMAIL PROTECTED] wrote:
 I'm stuck this, and would appreciate help or pointers.
 
 I have a form with 2 selection drop-downs. I want the user's choice of
 select 1 to drive the options displayed in select 2. When the user
 makes a selection in select 1, onChange calls a Javascript function
 that should write select 2 options based on the result of a call to a
 Python script, passing the select 1 choice as an argument to the Python
 script.
 
 But: I cannot get the Javascript var into the namespace that the TALES
 expression knows about.
 
 Example:
 
 script
 function makeDropDown() {
   var widget = document.getElementById('select1');
   var choice = widget.options[widget.selectedIndex].value;
   var data = [result of calling Python script 'foo(arg)' with
 arg=choice]
   // create options for select 2
 }
 /script
 [various html...]
 select id=select1 onChange=makeDropDown()
 [options...]
 /select
 
 I've tried with multiple scripts, where one has tal:content=... but
 cannot figure out how the TALES expression can get at what my
 javascript gets from an event. I haven't done much with JavaScript
 inside Page Templates, so perhaps I'm going about this all wrong.
 
 TIA,
 Rob
 
 
 
 
 __
 Discover Yahoo!
 Stay in touch with email, IM, photo sharing and more. Check it out!
 http://discover.yahoo.com/stayintouch.html
 ___
 Zope maillist  -  Zope@zope.org
 http://mail.zope.org/mailman/listinfo/zope
 **   No cross posts or HTML encoding!  **
 (Related lists -
  http://mail.zope.org/mailman/listinfo/zope-announce
  http://mail.zope.org/mailman/listinfo/zope-dev )
 


-- 
Peter Bengtsson, 
work www.fry-it.com
home www.peterbe.com
hobby www.issuetrackerproduct.com
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists -
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] TAL and Javascript

2005-06-29 Thread Lennart Regebro
On 6/29/05, Rob Boyd [EMAIL PROTECTED] wrote:
 But: I cannot get the Javascript var into the namespace that the TALES
 expression knows about.

TALES are executed on the server, when the template is rendered.
Javascript is executed on the client after the template has rendered,
and hence, well after the TALES have been executed.

So, no you can't do that without making a new roundtrip to the server.
You have two options: Either use the Javascripts onClick to make an
XML call to the server to fetch the new data (not trivial) or send all
the data from the start.

This last thing is what almost all sites do with all that type of
Javascript functionality.

-- 
Lennart Regebro, Nuxeo http://www.nuxeo.com/
CPS Content Management http://www.cps-project.org/
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists -
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] TAL and Javascript

2005-06-29 Thread J Cameron Cooper

Rob Boyd wrote:

I'm stuck this, and would appreciate help or pointers.

I have a form with 2 selection drop-downs. I want the user's choice of
select 1 to drive the options displayed in select 2. When the user
makes a selection in select 1, onChange calls a Javascript function
that should write select 2 options based on the result of a call to a
Python script, passing the select 1 choice as an argument to the Python
script.

But: I cannot get the Javascript var into the namespace that the TALES
expression knows about.

Example:

script
function makeDropDown() {
  var widget = document.getElementById('select1');
  var choice = widget.options[widget.selectedIndex].value;
  var data = [result of calling Python script 'foo(arg)' with
arg=choice]


This is impossible, at least in a single request. You are trying to mix 
server-side and client-side actions.


You would have to make a new request on the server with the contents of 
'choice' in order to get it processed. This can be done with XML-RPC or 
some other options; look up 'AJAX' to get a feel for this.


Alternately, if you have a known list of values for 'choice' you could 
generate all the 'data' values for each 'choice' value, encode them in 
the page's javascript, and look them up client-side.


--jcc

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

Enfold Systems, LLC
http://www.enfoldsystems.com
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] TAL and Javascript

2005-06-29 Thread David H

Rob,
As others have pointed out the approach you indicate will not work.
To summarize -
Write python script to dynamically create JAVASCRIPT code (you can 
format javascript arrays of data from your db this way). 


Then, using TAL load it in, eg
head
tal:js replace=structure 
here/python/pythonThatCreatedJavaScriptjavascript here/tal:js

/head

Which is then callable from the other javascript routines in your page.

David



Rob Boyd wrote:


I'm stuck this, and would appreciate help or pointers.

I have a form with 2 selection drop-downs. I want the user's choice of
select 1 to drive the options displayed in select 2. When the user
makes a selection in select 1, onChange calls a Javascript function
that should write select 2 options based on the result of a call to a
Python script, passing the select 1 choice as an argument to the Python
script.

But: I cannot get the Javascript var into the namespace that the TALES
expression knows about.

Example:

script
function makeDropDown() {
 var widget = document.getElementById('select1');
 var choice = widget.options[widget.selectedIndex].value;
 var data = [result of calling Python script 'foo(arg)' with
arg=choice]
 // create options for select 2
}
/script
[various html...]
select id=select1 onChange=makeDropDown()
[options...]
/select

I've tried with multiple scripts, where one has tal:content=... but
cannot figure out how the TALES expression can get at what my
javascript gets from an event. I haven't done much with JavaScript
inside Page Templates, so perhaps I'm going about this all wrong.

TIA,
Rob







___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


[Zope] TAL and Javascript

2005-06-29 Thread Rob Boyd
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.

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. If my Python
script didn't take an argument, I'd have no problem. I could do this
and use javascript to build the select options from the appropriate
values. But I'd like to cut down on what the browser has to handle.
What the user chooses in the first selection can reduce what they get
to choose in selection two from 3000 choices to hundreds. I'd prefer to
have the backend reduce the result set rather than having the browser
do the work.

I thought about AJAX (no experience), but given time constraints, looks
like I'll have to load everything on the page and go from there.

Rob



 
Yahoo! Sports 
Rekindle the Rivalries. Sign up for Fantasy Football 
http://football.fantasysports.yahoo.com
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] TAL and Javascript

2005-06-29 Thread J Cameron Cooper

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
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] TAL and Javascript

2005-06-29 Thread Jonathan


- Original Message - 
From: Rob Boyd [EMAIL PROTECTED]

Thanks to all the responders. It gave me some ideas, but alas no luck.



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. If my Python
script didn't take an argument, I'd have no problem. I could do this
and use javascript to build the select options from the appropriate
values. But I'd like to cut down on what the browser has to handle.
What the user chooses in the first selection can reduce what they get
to choose in selection two from 3000 choices to hundreds. I'd prefer to
have the backend reduce the result set rather than having the browser
do the work.


We do this a lot... a few key concepts to get you going:

1) You can embed dtml tags  within javascript, eg:
 hiddenframe.location.href = 'dtml-var 
baseurlSub_GetPresentorPage?confid=dtml-var login_idSessUpd=1'


This allows you to write scripts which generate javascript code that is 
specific to the situation (when zope processes the dtml tags, the tags will 
be replaced with 'data' which then becomes part of the javascript code).



2) You can set javascript variables from dtml/scripts, eg:

   parent.presentorpage= 'dtml-var 
temp_folder.getProperty(confid+'_CurrSlide')'


This allows you to store zope objects/fields/etc in variables that 
javascript can then use.



3) We use hidden html frames to provide a location that javascript can 
reload in order to make 'invisible' (not really, but close enough) calls 
from the browser to the server



hth

Jonathan



___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )