How to import jquery

37 views
Skip to first unread message

Craig Small

unread,
Apr 21, 2014, 4:14:39 AM4/21/14
to TurboGears .
I know that importing things is a little tricky in TG2 but what is the
"TG2 way" to import jquery?

tg.url doesn't work because its hiding under tw2.jquery.
Can I just do something like
from tw2.jquery import jquery_js
and directly send it out from my controller with
<script src=${thing_the_controller_sends} type="text/javascript">

In my master template?
--
Craig Small (@smallsees) http://enc.com.au/ csmall at : enc.com.au
Debian GNU/Linux http://www.debian.org/ csmall at : debian.org
GPG fingerprint: 5D2F B320 B825 D939 04D2 0519 3938 F96B DF50 FEA5

Mengu

unread,
Apr 21, 2014, 6:30:55 PM4/21/14
to turbo...@googlegroups.com, csm...@enc.com.au
yes you can but there's another way.

http://tw2core.readthedocs.org/en/latest/design/ see the section "using your own resources." you can have a jquery file under public/js and make toscawidgets use it.

Craig Small

unread,
Apr 22, 2014, 8:11:22 AM4/22/14
to turbo...@googlegroups.com
On Mon, Apr 21, 2014 at 03:30:55PM -0700, Mengu wrote:
> yes you can but there's another way.
>
> http://tw2core.readthedocs.org/en/latest/design/ see the section "using
> your own resources." you can have a jquery file under public/js and make
> toscawidgets use it.
That's good doco, I was after the TG2 way not the TW2 but I'll file
that away for future reference.

Actually TG2 does some sort of jquery thing, but I think this is wrong.
Have a look at:
https://github.com/TurboGears/tg2devtools/blob/master/devtools/commands/quickstart_mako/master.mak

<script src="http://code.jquery.com/jquery.js"></script>
line 16

I would of thought external resources were, by default, not a good idea?
An option, sure, but not the default.

- Craig

Craig Small

unread,
Apr 24, 2014, 8:48:48 AM4/24/14
to turbo...@googlegroups.com
On Tue, Apr 22, 2014 at 10:11:22PM +1000, 'Craig Small <csm...@enc.com.au>' via TurboGears wrote:
> <script src="http://code.jquery.com/jquery.js"></script>
Now I know what Alessandro was going on about about resource loading and
what a pain it is.

You don't load jquery, menus don't work.
You unconditionally load jquery, jqgrid doesn't work.

I had to put this sort of evilness in to get both happy:
<script>window.jQuery || document.write("<script
src=\"${tg.url('/javascript/jquery.min.js')}\">\x3C/script>");</script>

It might be a ToscaWidgets bug tripping up jqgrid, not sure.

Mengu

unread,
Apr 25, 2014, 10:20:54 AM4/25/14
to turbo...@googlegroups.com, csm...@enc.com.au
i've always been skeptical to generating frontend code from the backend. if i liked that, i'd use gwt, zk or vaadin. maybe you don't want to deal javascript then i'd suggest using coffeescript with emberjs. (go ahead if angularjs is more appealing to you) there is also clojurescript if you're into lisp / clojure. once your backend is only for backend, you'll be much more happier. nothing will get away or bang your head against the table for hours.

Alessandro Molina

unread,
Apr 29, 2014, 12:40:41 PM4/29/14
to TurboGears .
You probably want to do the opposite, while it's less flexible, is usually easier to implement.

Instead of adding jQuery from HTML and check collision with tw2.jquery, you can import jquery_js from tw2.jquery and call jquery_js.inject() inside your BaseController.__call__ or inside a before_render application global hook.

That will always ensure that you have jQuery available in your templates and will let TW aware of the fact that it is already available.


--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email to turbogears+...@googlegroups.com.
To post to this group, send email to turbo...@googlegroups.com.
Visit this group at http://groups.google.com/group/turbogears.
For more options, visit https://groups.google.com/d/optout.

Craig Small

unread,
Apr 29, 2014, 11:06:52 PM4/29/14
to TurboGears .
On Tue, Apr 29, 2014 at 06:40:41PM +0200, Alessandro Molina wrote:
> That will always ensure that you have jQuery available in your templates
> and will let TW aware of the fact that it is already available.
Yes, that is exactly what I'm looking for.

- Craig
Reply all
Reply to author
Forward
0 new messages