Hi Christoph, If I've understood the code correctly, you're hard-wiring the callback method name via the template name ... but I'd like to be able to read the callback method name from the request parameters (I'm calling it from some jQuery code - that sends in dynamically created callback method names by default, which I'd like to be able to handle in the renderer). Hence the:
template_vars['tg']['inputs']['jsoncallback'] in my original solution. Is there a cleaner way of doing this? Thanks very much in advance for any further enlightenment you can send my way :D Cheers, C On Oct 23, 2:08 pm, Christoph Zwerschke <[email protected]> wrote: > Am 23.10.2010 01:45 schrieb [email protected]: > > > Also, I'm not entirely happy with the way I'm excluding the TG dict > > keys in the render function - so if anyone wants to chip in a better > > solution, I'd be extremely grateful ;) > > Yes, that's really not nice. A somewhat better solution is requesting > the keys to be removed from tg, instead of using the hardcoded list. > > Also, JSONP controllers should deliver a 'text/javascript' mime type > instead of the default 'text/html'. > > > NOTE: the current implementation presupposes that you pass in the > > JSONP callback method in the URL parameter 'jsoncallback'. E.g. > >http://my.site.com/path/to/controller?jsoncallback=mycallback > > In addition to that, you could abuse the template_name for defining a > default for the callback function on the server side, and add a fixed > default in case it is not set via template name or parameter. > > Example: expose('jsonp:mycallbackmethod') > > So my improved solution looks like this: > > from tg import json_encode, response > from tg.render import _get_tg_vars > > def render_jsonp(template_name, template_vars, **kwargs): > callback = (template_name > or kwargs.pop('callback', None) or 'callback') > for key in _get_tg_vars(): > del template_vars[key] > response.headers['Content-Type'] = 'text/javascript' > return '%s(%s)' % (template_name, json_encode(template_vars)) > > from myapp.config.app_cfg import base_config > base_config.render_functions['jsonp'] = render_jsonp > base_config.mimetype_lookup = {'.jsonp': 'text/javascript'} > > I've also added ahttp://trac.turbogears.org/ticket/2513for this. > > -- Christoph -- 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.

