Am 08.07.2010 um 04:59 schrieb Shane:
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).
No. Your debugging showed you Python-values. No JSON nowhere. They
might be destined to become converted to JSON somewhere, I have no idea.
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.
Again, NO. d is a Python-dictionary (actually something else, but
that's a technicality) containing whatever values you passed.
Including grid_options, which by itself is *also* a Python dictionary.
So far no conversion to JSON has been performed.
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.
Well, given that you don't do any JSON-conversion, there is a better
way. Actuall *do* convert something to JSON.
What you've done so far is simply relying on the string-representation
of a Python dictionary to be very similar to what JSON looks like. But
only similar - the differences are e.g. booleans, and also some finer
aspects of unicode-encoding which you so far didn't suffer from.
So, all you need is a simple
.... })""" % (dumps(grid_options))
And no, it's not doubling any effort. Because if you do this (pure
Python, no JSON nowhere):
>>> d = {"a" : "b", "c" : 100}
>>> "%s" % d
"{'a': 'b', 'c': 100}"
what Python does, it creates a string-representation of a dict. And to
do so it essentially does this:
def print_dict(d):
print "{"
for key, value in d.iteritems():
print "%s : %s" % (str(key), str(value))
print "}"
And now guess what simplejson.dumps does... the same, just instead of
calling str on keys and values, it calls another function that knows
how to format strings and booleans according to what JSON specifies.
There is no noticable runtime-penalty here.
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.