[three]Bean
Hacking tw2 resource injection
Dec 13, 2011 | categories: python, toscawidgets, turbogears View CommentsTonight, VooDooNOFX was asking in IRC in #turbogears how to disable
the injection of jquery.js by tw2.jquery into her/his TG2 app. Using the
inject_resources=False
middleware config value wouldn't cut it,
since she/he wanted tw2 to inject all other resources, they were
loading jQuery via google CDN beforehand and tw2's injection was clobbering
their code.
I came up with the following hack to myapp/lib/base.py
which
will remove tw2.jquery.jquery_js from the list of resources tw2 would inject
into each page served by a TG2.1 app.
At the top of myapp/lib/base.py
import:
import tw2.core.core import tw2.jquery
and then replace:
return TGController.__call__(self, environ, start_response)
with the following:
stream = TGController.__call__(self, environ, start_response) # Disable the injection of tw2.jquery offending_link = tw2.jquery.jquery_js.req().link local = tw2.core.core.request_local() local['resources'] = [ r for r in local.get('resources', list()) if r.link != offending_link ] return stream
The two tricks to this are
- Simply knowing that tw2 resources register themselves with the 'request_local' object and that during the return-phase of the WSGI pipeline, the tw2 middleware refers to that list when injecting resources
- Figuring out where in a TG2 app's request flow to place the call to alter that object after all widgets that might register jquery have declared their resources but before the resources list is injected into the output stream.
We came out of it with a bug filed in the tw2 issue tracker so we can take care of it properly in the future.