Better and more powerful:
class JQuery:
def __init__(self,name,attr=None,*args):
self.name=name
self.attr=attr
self.args=args
def __str__(self):
import gluon.contrib.simplejson as json
def encode(obj):
if isinstance(obj,JQuery): return str(obj)
return json.dumps(obj)
if not self.attr:
return '$("%s")' % self.name
args=', '.join([encode(a) for a in self.args])
return '%s.%s(%s)' % (self.name, self.attr, args)
def __getattr__(self,attr):
def f(*args):
return JQuery(self,attr,*args)
return f
print JQuery('test').set(1,'',{},JQuery('#12').val()).get(45)
On Feb 11, 11:56 pm, mdipierro <[email protected]> wrote:
> Try something like this:
>
> class JQuery:
> def __init__(self,name,attr=None,*args):
> self.name=name
> self.attr=attr
> self.args=args
> def __str__(self):
> if not self.attr:
> return "$('%s')" % self.name
> import gluon.contrib.simplejson as json
> args=', '.join([json.dumps(a) for a in self.args])
> return "%s.%s(%s)" % (self.name, self.attr, args)
> def __getattr__(self,attr):
> def f(*args):
> return JQuery(self,attr,*args)
> return f
>
> print JQuery('test').set(1,"",{}).get(45)
>
> On Feb 11, 11:37 pm, "mr.freeze" <[email protected]> wrote:
>
> > All together now... (gluon/clientutils.py)...
>
> > class PageManager(object):
> > def __init__(self, response):
> > self.response = response
> > self.response.scripts=[]
> > self.response.pagescripts=[]
> > self.response.stylesheets=[]
> > def addscript(self,script):
> > if not script in self.response.scripts:
> > self.response.scripts.append(script)
> > def addpagescript(self,pagescript):
> > if not pagescript in response.pagescripts:
> > response.pagescripts.append(pagescript)
> > def addstylesheet(self,stylesheet):
> > if not stylesheet in response.stylesheets:
> > response.stylesheets.append(stylesheet)
> > """
> > The three methods below should not be used because
> > they require an instance variable in
> > the view making it non-generic. Here for testing.
> > """
> > def includescripts
> > scripts = ''
> > for script in self.response.scripts:
> > scripts += script
> > return scripts
> > def includepagescripts
> > pagescripts = ''
> > for file in self.response.pagescripts:
> > pagescripts += SCRIPT(_src=file).xml()
> > return pagescripts
> > def includestylesheets
> > stylesheets = ''
> > for file in self.response.stylesheets:
> > stylesheets += LINK(_href=file,
> > _rel="stylesheet",
> > _type="text/css",
> > _charset="utf-8").xml()
> > return stylesheets
>
> > Now if I could just mimic jQuery's sweet chaining in Python...
>
> > On Feb 11, 3:55 pm, "mr.freeze" <[email protected]> wrote:
>
> > > Sure. I think it would look something like this (in gluon/
> > > clienttools.py):
>
> > > class PageManager(object):
> > > def __init__(self, response):
> > > if not response.scripts: response.scripts = []
> > > def addscript(self,script):
> > > response.scripts.append(script)
>
> > > Then in web2py_ajax.html as the last thing in the $(document).ready
> > > function:
>
> > > {{if response.scripts:}}
> > > {{for s in response.scripts:}}
> > > {{=s}}
> > > {{pass}}
> > > {{pass}}
>
> > > Then in your db.py model:
>
> > > from gluon.clienttools import PageManager
> > > pagemanager=PageManager(response)
>
> > > Then in your controller:
>
> > > def index():
> > > pagemanager.addscript("$('test').hide();")
> > > response.flash=T('Welcome to web2py')
> > > return dict(message=T('Hello World'))
>
> > > Ideally, the clienttools module would be chocked full of jquery
> > > helpers too :)
>
> > > On Feb 11, 1:40 pm, mdipierro <[email protected]> wrote:
>
> > > > Can you write the addscript function?
>
> > > > On Feb 11, 1:31 pm, "mr.freeze" <[email protected]> wrote:
>
> > > > > Good stuff. That reminds me...I think a good addition to web2py would
> > > > > be some kind of page/script manager (like t2's response.files on
> > > > > steroids). Just thinking off the top of my head here but...
>
> > > > > response.stylesheets (list rendered in the head of we2py_ajax.html)
> > > > > response.pagescripts(list rendered in the head of we2py_ajax.html)
> > > > > response.scripts(list rendered after the end of $(document).ready)
>
> > > > > pagemanager.includestylesheet(path_to_static_css)
> > > > > pagemanager.includescript(path_to_static_js)
> > > > > pagemanager.addscript(your_script_snippet)
>
> > > > > Each pagemanager method would check to see if the file/snippet already
> > > > > exists in the corresponding response.[stylesheets,pagescripts,scripts]
> > > > > before adding to prevent duplicates.
>
> > > > > Then some jQuery helpers to abstract common selectors,manipulators and
> > > > > effects so you could do:
>
> > > > > pagemanager.addscript(jquery.getbyid('testid').fadein('slow'))
> > > > > pagemanager.addscript(jquery.getbyclass('testclass').setattribute
> > > > > ('src','/images/hat.gif'))
>
> > > > > Can you tell I'm bent on programming everything in Python?
>
> > > > > On Feb 11, 11:26 am, mdipierro <[email protected]> wrote:
>
> > > > > > do not miss these:
>
> > > > > >http://devsnippets.com/reviews/using-jquery-to-style-design-elements-...
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web2py Web Framework" 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/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---