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 a http://trac.turbogears.org/ticket/2513 for 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.

Reply via email to