Hi Diez!
I thought more about what I was doing after reading the part of your
post questioning my use of repr. I was using repr to output debugging
info on parameters passed from my controller to my template
<div id="menuObj"></div>
${tmpl_context.form(value = value, child_args = child_args,
passed=passed) }
<p> Grid Options </p>
<p>${repr(value)}</p>
<p>In TableView.html: ${repr(passed)}</p>
where <p>$[repr(value)} gives, among many other things:
'pager_opts': [{'edit': False, 'addfunc': 'addURLHandler', 'add':
True, 'del': True}, {'reloadAfterSubmit': False, 'height': 280},
{'reloadAfterSubmit': False, 'height': 280}, {'reloadAfterSubmit':
False, 'height': 280}, {'reloadAfterSubmit': False, 'height': 280},
{'reloadAfterSubmit': False, 'height': 280}]...
Note the True boolean values. So, ${repr(value)} dumps whatever JSON
came over from the controller into a Python string. Using FireBug, I
can verify that the JSON coming from the controller is correct,
including the booleans. So I see why I though the JSON was messed
up-- my debugging was actually showing me Python representations
(True) of the JSON booleans (true).
So, my debugging method isn't so good, but I still don't how to pass
booleans over to widgets for use within JavaScript. For example, a
snippet from my jqGrid widget that initialized the jqGrid upon page
load:
def update_params(self, d):
super(Jqgrid, self).update_params(d)
grid_values = d['value'][self.id]
grid_options = grid_values['grid_options']
init_js = '''
...
jQuery(document).ready(function(){
options = %s; // Loads options from
//passed d into
Javascript
if (options.dblClickURL) {
options.ondblClickRow = dblClickRow;
}
if (options.sngClickURL) {
options.onSelectRow = sngClickRow;
}
this_grid = jQuery(grid_dom_id).jqGrid (options);
.... more stuff
})""" % ( grid_options)
self.add_call(init_js)
So d is a Python dictionary that is a representation of the JSON from
my controller. Passing strings and ints works fine, but booleans will
convert to Pythonic True & False. Passing values from d to my
Javascript code using %s is going to give me errors within my JS,
since JS is going to contain True and False, not true and false.
So, this is not a JSON conversion problem. The data from the
controller is fine. My new question is how can I pass Python booleans
to JS code that I am building as strings within my widgets? I can
only think of two options:
1) The True=true, False=false constant conversion. Not pretty and a
little dangerous, but it works and its fast
2) Doing yet another JSON conversion -> json.dumps(json.load(dic)) to
%s. This is probably the equivalent of looping through the entire
dictionary yet again which I would rather avoid. There must be a
better way.
Thanks,
Shane
On Jul 6, 3:14 am, "Diez B. Roggisch" <[email protected]> wrote:
> 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.