Am 06.07.2010 um 09:30 schrieb Shane:

Hello,

This is a follow-up to another post I put up back in January regarding
how to pass Boolean values to and from Javascript (Search "Clever
Boolean JS JSON" for background).  Several people in the group gave me
advice (thanks, guys!), but I was never able to put together a
solution and had to write a different template every time I wanted to
set a new arrangement of booleans within the JS.  Since this posting,
I have learned how to build TW widgets, and have been writing a widget
for the jqGrid library so that I can reduce about these view-specific
templates into a single template.  Again, the problem with passing
booleans appeared.

To sum up the problem, passing this dict from TG (using
@expose('json')):

... other stuff ... dict(id='name', name='Name', width=100,
sorttype='text', align='left', editable=True) ...

gives this in the template (output using <p>${repr(value)}</p>)

..other stuff... {'sorttype': 'text', 'name': 'Name', 'align': 'left',
'editable': True, 'width': 100, 'id': 'name'} ...

^^^^^^^^^^^^^^
The problem is that there is no True in Javascript.  I tried passing
various strings ('' for false, anything else for true), hoping
somewhere within jqGrid it would be cast as a Boolean, but no good.
Finally, right before punting and writing loops to test every single
key, value pair, I came up with this, placed into the add_call JS of
my widget:

                const True = true;
                const False = false;


If this is obvious, then please ignore this post, but it certainly
took me a long time of banging my head against a wall to figure it
out.  Fortunately, I need this to happen within only a small isolated
bit of code, so scope shouldn't be a big problem.

It is *not* obvious. While working, it's simply wrong because - as you say yourself - you can't possibly change all kinds of JS to allow for this.


I still don't understand why a Python True ends up as a True in the
Javascript.  Doing the same thing manually using the simplejson
library (Which is what TurboJSON is based on):

In [1]: import simplejson as json
In [2]: test = dict(id='name', name='Name', width=100,
sorttype='text', align='left', editable=True)
In [3]: json.dumps(test)
Out[3]: '{"width": 100, "sorttype": "text", "name": "Name", "align":
"left", "editable": true, "id": "name"}'

It's a string output, but if json.dumps evaluates to "editable":true
why do I get "editable":True in my template?

I have no idea, but I am pretty sure you must be doing something very weird.

dumps works perfectly fine for me in e.g. widgets.

But in the above code, you talk about templates and repr. What should these be for? When I want a JSON-controller in TG2, all I do is this:


@expose("json")
def test(self):
    return dict(key=True, other_key=False)


And that ends up as a proper JSON-request.

Please show a bit more code of what you are really doing.

Diez


Diez



--
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en.

Reply via email to