[Grok-dev] jQuery Ajax and Grok

Martijn Faassen faassen at startifact.com
Tue Jul 13 12:16:12 EDT 2010


Hello,

On 07/11/2010 09:39 AM, Daniel Lyons wrote:
> <input type="checkbox" tal:define="check_url
> python:view.url('check')" tal:attributes="onclick
> string:checkItem('${check_url}?list_index=${repeat/todolist/index...
>
> In my code, to make things unobtrusive, I have the following:
>
> <input class="check" type="checkbox" name="items:list"
> tal:attributes="checked item/checked"/>
>
> Then in my todo.js file I have some code that basically looks like
> this:
>
> $('.check').change(function(e){ $.get('check'... });
>
> What's bugging me is that I have hard-coded 'check' URL in the
> jQuery. I'd rather use Grok's URL, but I'd like to remain
> unobtrusive. Is there a good solution to this problem?

The URL generated in the page template example is inserted by the page 
template engine, making use of the state of objects on the server.

The URL generated in the jquery example needs to use a URL that only the 
server knows. So you need to output it somewhere in the HTML. For 
instance an invisible div somewhere:

<div class="invisible" tal:content="check_url"></div>

(imagining that there's a class 'invisible')

And then pick this up separately and in your javascript construct the 
URL from the check URL and (somehow) figure out the index in the 
checkboxes as well.

Alternatively you can generate the complete check URL in an invisible 
div just above the <input> box, or (but I'm not sure how compatible this 
is) in an attribute on the 'input' and retrieve this with jquery.

Without some information in the HTML it's going to be harder to solve. 
Here I go into some more abstract thought about it:

I've run into this problem before in a different context. In that 
situation I had all the information that was going to go into the 
template in an JSON datastructure, and moreover, the client had access 
to this structure (along with a template and a client-side template 
engine called json-template).

This meant that in json-template terms, each HTML element had a certain 
context of local variables associated with (for instance your 
check_url). I hacked up an extension to json-template at the time that 
would help the developer retrieve this information within a handler like 
the one you're writing. One day I intend to revive this code and package 
it up. Doing client-side templating does change ones style of 
development quite a bit though, and is not appropriate for all applications.

I hope this helps!

Regards,

Martijn



More information about the Grok-dev mailing list