Mochikit in turbogears 2.x

5 views
Skip to first unread message

Thang Nguyen

unread,
Nov 22, 2009, 4:35:49 PM11/22/09
to turbo...@googlegroups.com
Hi,

I added the line below into app_cfg.py for enabling Mochikit, but kept
on getting error.

This is the new line added to app_cfg.py:

tg.include_widgets = ['turbogears.mochikit']

The error I got:
from helloworld.config.app_cfg import base_config
File "C:\turbogears\sandbox\HelloWorld\helloworld\config\app_cfg.py", line 66,
in <module>
tg.include_widgets = ['turbogears.mochikit']
NameError: name 'tg' is not defined

Any suggestion on how this works is much appreciated.
thanks
tpn

Diez B. Roggisch

unread,
Nov 23, 2009, 2:49:37 AM11/23/09
to turbo...@googlegroups.com
Thang Nguyen schrieb:
> Hi,
>
> I added the line below into app_cfg.py for enabling Mochikit, but kept
> on getting error.
>
> This is the new line added to app_cfg.py:
>
> tg.include_widgets = ['turbogears.mochikit']
>
> The error I got:
> from helloworld.config.app_cfg import base_config
> File "C:\turbogears\sandbox\HelloWorld\helloworld\config\app_cfg.py", line 66,
> in <module>
> tg.include_widgets = ['turbogears.mochikit']
> NameError: name 'tg' is not defined

This is only worknig in TG1.

There is no direct equivalent for this in TG2.

First of all, you need to install tw.mochikit. Then the normal procedure
is to write a ToscaWidget that declares a dependency to that widget.

As a result, the tw.mochikit will be automatically injected into the page.

Alternatively, you can call .inject() on the respective widget youself.
I don't use tw.mochikit, but for e.g. jquery, this works roughly like this:


from tw.jquery import jquery_js


class MyController(...):


@expose(...)
def action(self):
jquery_js.inject()
...



If you are a 100% sure you want MK with *every* page, then you should
consider placing the inject into your BaseController.__call__.

Diez

Thang Nguyen

unread,
Dec 14, 2009, 2:30:46 PM12/14/09
to turbo...@googlegroups.com
Hi Diez,

I followed your suggestion and was able to inect jquery_js to my
controller. However, I am running into a problem. Below is the
sample code. I expect to see the time (id=timetest) to be updated
everytime I click on "get time" link; however, it does not. Hope you
can shed a light on it. Thanks much in advance. -tpn

From my controller (root.py):
=====================

@expose('helloworld.templates.about')
def about(self, **kw):
import time
jquery_js.inject()
return dict(now=time.ctime())

@expose()
def time(self):
import time
return dict(now=time.ctime())


From my template:
==============
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/"
xmlns:xi="http://www.w3.org/2001/XInclude">

<xi:include href="master.html" />

<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type"
py:replace="''"/>
<title>Learning TurboGears 2.0: Quick guide to the Quickstart pages.</title>
<script type="text/javascript">
$(document).ready(function(){
$('a').click(function(event){
$('#timetest').load("/time")
});
});

</script>
</head>

<body>
<div id="timelink"><a href = "#">get time</a></div>
<div id="timetest" py:replace="now"></div>
</body>
</html>
> --
>
> You received this message because you are subscribed to the Google Groups "TurboGears" group.
> To post to this group, send email to turbo...@googlegroups.com.
> To unsubscribe from this group, send email to turbogears+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/turbogears?hl=.
>
>
>

Diez B. Roggisch

unread,
Dec 14, 2009, 3:27:59 PM12/14/09
to turbo...@googlegroups.com
Hi,


> I followed your suggestion and was able to inect jquery_js to my
> controller. However, I am running into a problem. Below is the
> sample code. I expect to see the time (id=timetest) to be updated
> everytime I click on "get time" link; however, it does not. Hope you
> can shed a light on it. Thanks much in advance. -tpn
>
> From my controller (root.py):
> =====================
>
> @expose('helloworld.templates.about')
> def about(self, **kw):
> import time
> jquery_js.inject()
> return dict(now=time.ctime())
>
> @expose()
> def time(self):
> import time
> return dict(now=time.ctime())

Any reason you don't import time *once* on top of the module? It doesn't
change the semantics, but local imports should only be used when there
is good reason - circular dependencies or lazy loading.
Two questions:

- what does FireBug say? Does the call work, is there real network
activity?
- some browsers aggressively cache the requests, so it's a good idea
to randomize or add a timestamp to queries like load("/time").

Diez

Thang Nguyen

unread,
Dec 14, 2009, 7:59:50 PM12/14/09
to turbo...@googlegroups.com
Thanks for your email. Still could not get it to work. I probably
do not understand well how this thing (jquery/turbogear/cache)
supposed to work. I will dig into it further, any additional
suggestion is greatly appreciated. -tpn

My answers to your questions:
======================

(1) There is no particular reason to use local import, I need to clean up that.

(2) The call (load) works fine, as shown in the output, but the
replacement of "timetest" does not happen when "get time" is clicked.
I also modified the code to add a random number to the query such as
load("/time?sid=" + Math.random())... but does not help though.

Below are the sample code and the output (btw, I tried this on both
Firefox and IE, no difference). Also I did not see any difference
between using $('timetest') versus $('#timetest')

template:
=======
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/"
xmlns:xi="http://www.w3.org/2001/XInclude">

<xi:include href="master.html" />

<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type"
py:replace="''"/>
<title>Learning TurboGears 2.0: Quick guide to the Quickstart pages.</title>
<script type="text/javascript">
$(document).ready(function(){
$('a').click(function(event){
$('#timetest').load("/time?sid=" + Math.random())
});
});

</script>
</head>

<body>
<div id="timelink"><a href = "#">get time</a></div>
<div id="timetest" py:replace="now"></div>
</body>
</html>


Controller (root.py)
==============
@expose('helloworld.templates.about')
def about(self, **kw):
jquery_js.inject()
return dict(now=time.ctime())

@expose()
def time(self, sid):
print "time is called ...sid=%s" % sid
return dict(now=time.ctime())


Output (server console):
=================
serving on 0.0.0.0:8080 view at http://127.0.0.1:8080
time is called ...sid=0.7600414370879561
time is called ...sid=0.08766271084959143
time is called ...sid=0.9154010756223916
time is called ...sid=0.13931412162353551
time is called ...sid=0.9629765981657245
time is called ...sid=0.19117089588204772
time is called ...sid=0.6440978896256766
time is called ...sid=0.550432448440006
time is called ...sid=0.9023927250869724
time is called ...sid=0.1985474145942756
time is called ...sid=0.14242761973297957
time is called ...sid=0.5346612822522109
time is called ...sid=0.6367426755820135


tpn
> --
>
> You received this message because you are subscribed to the Google Groups "TurboGears" group.
> To post to this group, send email to turbo...@googlegroups.com.
> To unsubscribe from this group, send email to turbogears+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/turbogears?hl=en.
>
>
>

Diez B. Roggisch

unread,
Dec 15, 2009, 4:25:45 AM12/15/09
to turbo...@googlegroups.com
Thang Nguyen schrieb:
This looks as if you *replace* the div with whatever now contains. So
there is no div anymore, and then of course the jquery-statement
referring to the id of the div wont' have anything to work on.

Make that "py:content" instead, and you are good to go.

Diez

Thang Nguyen

unread,
Dec 15, 2009, 2:01:48 PM12/15/09
to turbo...@googlegroups.com
Thanks for the info. I tried it and still could not get it right...
For some reasons, with py:content, it displays the string "now" when
"get time' is clicked, but not the actual time. If the page
(about.html) is loaded/reloaded, it shows the time correctly though,
but not by cliking the "get_time".

I checked the time function in the controller (root.py), seems to be
fine there. I reviewed Jenshi template doc, but could not find
anything which might have caused this.

Changes to template using (py:content):
=============================

<body>
<div id="timelink"><a href = "#">get time</a></div>
<div id="timetest" py:content="now"></div>
</body>

Output from the browser:
==================
get time <== link
now <== this should display the time, but in this case it just
shows the string "now"

tpn
Reply all
Reply to author
Forward
0 new messages