I like it. I would call Function JFunction instead.
Massion
On Feb 13, 11:01 am, "mr.freeze" <
nfre...@gmail.com> wrote:
> What about this? '()' at the end of a JQuery statement terminates the
> line (;). Also added a simple Function wrapper class. Treats 'this' as
> a keyword.
>
> Mind you, there is a good chance that these helpers are more difficult
> that just writing jQuery code :)
>
> So this:
> print JQuery('#test').click(Function(JQuery("this").hide("slow")
> (),'event'))()
> print JQuery("p").bind("click",Function(JQuery("span").text("Click
> happened!")(),'e'))()
>
> Returns this:
> $('#test').click(function(event){$(this).hide("slow");});
> $('p').bind("click", function(e){$('span').text("Click happened!");});
>
> 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)
> if isinstance(obj,Function): return str(obj)
> return json.dumps(obj)
> if not self.attr:
> return ("$('%s')" %
self.name).replace
> ("'this'","this")
> args=', '.join([encode(a) for a in self.args])
> return '%s.%s(%s)' % (
self.name, self.attr, args)
> def xml(self):
> raise AttributeError
> def __getattr__(self,attr):
> def f(*args):
> return JQuery(self,attr,*args)
> return f
> def __call__(self,*args):
> if not args:
> jq = str(JQuery(self))
> jq = jq[3:]
> jq = jq[:-2]
> return jq + ";"
>
> class Function:
> def __init__(self,body,*args):
> self.body = body
> self.args = args
> def __str__(self):
> args=', '.join([str(a) for a in self.args])
> body=self.body
> return "function(" + args + "){" + body + "}"
> > This is not an easy one.
>
> > On Feb 12, 10:50 am, "mr.freeze" <
nfre...@gmail.com> wrote:
>
> > > Maybe a static methodJQuery.function(body, *args) that just wraps a
> > >JQueryobject with function(args){body}? There's probably a better
> > > way.
> > > You would also need to add logic to handle the 'this' keyword and line
> > > termination I think.
>
> > > On Feb 12, 9:28 am, mdipierro <
mdipie...@cs.depaul.edu> wrote:
>
> > > > Not quite. This works:
>
> > > > printJQuery("a").filter(".clickclass").click('xxx').end().filter
> > > > (".hideclass").click('yyy').end();
>
> > > > but here is no mechanism to pass a function yet....
>
> > > > Massimo
>
> > > > On Feb 12, 9:14 am, "mr.freeze" <
nfre...@gmail.com> wrote:
>
> > > > > OMG, you just portedJQueryto Python in about 20 lines of code.
> > > > > Speechless...
>
> > > > > Would this work for more complicatedjQuerylike this?:
> > > > > > >>> printJQuery('test').set(1,'',{},JQuery('#12').val()).get(45)
>
> > > > > > $('test').set(1, "", {}, $('#12').val()).get(45)
>
> > > > > > >>> print DIV(H1('clickme',_onclick=JQuery('#1').slideToggle()),P('bla '*10,_id=1))
>
> > > > > > <div><h1 onclick="$('#1').slideToggle()">clickme</h1><p id="1">bla bla
> > > > > > bla bla bla bla bla bla bla bla </p></div>
>
> > > > > > On Feb 11, 11:56 pm, mdipierro <
mdipie...@cs.depaul.edu> wrote:
>
> > > > > > > Try something like this:
>
> > > > > > > classJQuery:
> > > > > > > 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):
> > > > > > > returnJQuery(self,attr,*args)
> > > > > > > return f
>
> > > > > > > printJQuery('test').set(1,"",{}).get(45)
> > > > > > > > Now if I could just mimicjQuery'ssweet chaining in Python...
> ...
>
> read more »