Google Groups Home
Help | Sign in
delegating in jquery: problems
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Sudara  
View profile
 More options 27 Mar, 17:30
From: Sudara <markswilli...@gmail.com>
Date: Thu, 27 Mar 2008 10:30:05 -0700 (PDT)
Local: Thurs 27 Mar 2008 17:30
Subject: delegating in jquery: problems
Hey there!

I'm SUPER excited about event delegation, and I'm giving it a whirl
with jquery, using behaviours.

So, I've got something simmering like so

Track = $.klass({

  onclick: $.delegate({
    '.track_link' : function(e){ this.toggleOptions(e) },
    '.play_link' : function(e){ this.togglePlay(e) }
  })

  // then, i define toggleOptions and togglePlay and so on...

});

Basically, I'm creating a Track behavior. A track has some links in
it, including .track_link and .play_link. Ok, but I get stuck:

1. "e" doesn't seem to be the event, it seems to be the target - is
this correct or am I smoking crack? I don't really need the event (i
don't think), but in the example on the danwebb blog, you explicitly
pass e.target.

2. In these two functions that I'm calling (like toggleOptions) if I
return false in the method, the event continues to bubble. Is this
right? I can change it to be

return this.toggleOptions(e)

with toggleOptions or even manually return false, in which case then
the event stops bubbling.

and this brings me to...

3. For some odd reason, I can only match against 1 selector. So, in
the above example, I can match either .track_link OR .play_link,
basically, whichever comes first.

HALP!

I'm very excited by what I *could* be doing once I get this working,
but something seems fishy here (else I'm having a bad coding day.)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sudara  
View profile
 More options 27 Mar, 17:32
From: Sudara <markswilli...@gmail.com>
Date: Thu, 27 Mar 2008 10:32:18 -0700 (PDT)
Local: Thurs 27 Mar 2008 17:32
Subject: Re: delegating in jquery: problems
A quick clarification for #2 - by "continues to bubble" i really mean,
the default action is executed.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sudara  
View profile
 More options 27 Mar, 21:21
From: Sudara <markswilli...@gmail.com>
Date: Thu, 27 Mar 2008 14:21:11 -0700 (PDT)
Local: Thurs 27 Mar 2008 21:21
Subject: Re: delegating in jquery: problems
Oh, and we're talking latest git release of lowpro for jquery as of
March 27, 2008 and jquery 1.2.3

On 27 Mar, 18:32, Sudara <markswilli...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sudara  
View profile
 More options 28 Mar, 16:38
From: Sudara <markswilli...@gmail.com>
Date: Fri, 28 Mar 2008 09:38:22 -0700 (PDT)
Local: Fri 28 Mar 2008 16:38
Subject: Re: delegating in jquery: problems
So, the latest version in git seems to cause issue #3

The version before works great:

http://github.com/danwrong/low-pro-for-jquery/tree/c1dc9b8f95f0bd40fe...

re: #2 I have been manually return false, or returning my function
which returns false and stops the default behaviour.

Syntactically, the existing:

  onclick: $.delegate({
    '.play_link' : function(e){ return this.togglePlay(e)},
    '.track_link': function(e){ return this.toggleOptions(e)}
  }),

would be even nicer (for my usage, at least) like so:

  onclick: $.delegate({
    '.play_link' :this.togglePlay(e),
    '.track_link': this.toggleOptions(e)
  }),

or heck, in behaviors, I often initialize and set some variables like
so:

initialize: function() {
    this.playLink = $(".play-button",this.element);
    this.trackLink = $("a.track_link",this.element);
  }

how cool would this be:

  onclick: $.delegate({
    this.playLink :this.togglePlay(),
    this.toggleLink: this.toggleOptions()
  })

Of course, I'm not great with the javascript metaprogramming, but heck
that would be the awesomist API.

Thanks again for your work Dan - it's opened my eyes to some great
techniques.

Sudara

On 27 Mar, 22:21, Sudara <markswilli...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sudara  
View profile
 More options 29 Mar, 18:38
From: Sudara <markswilli...@gmail.com>
Date: Sat, 29 Mar 2008 11:38:27 -0700 (PDT)
Local: Sat 29 Mar 2008 18:38
Subject: Re: delegating in jquery: problems
Ok,

In the end, I needed to satisfy the case that the source element
matched and the parent of the source matches with the precedence going
first to the source element.

http://pastie.caboo.se/172487

Now, I'm sure this isn't the most beautiful way of going about things,
but it works, giving priority to matching against the source element
before checking parents.

In a way, allowing the source element to be a child opens a can of
worms: I'm willing to bet that someone out there (or me soon) is going
to need even more fine-grained precedence matching, wanting the
closest event to the source to fire ;)

Anywho, this is fun, thanks for listening.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dan Webb  
View profile
 More options 29 Mar, 19:30
From: "Dan Webb" <d...@danwebb.net>
Date: Sat, 29 Mar 2008 19:30:59 +0000
Local: Sat 29 Mar 2008 19:30
Subject: Re: delegating in jquery: problems
Sorry, I've already got this checked in to my local git repos ready to
push out to github but I have my first son on tuesday so haven't had a
chance to read this list until now.  Still hopefully working out how
to fix it was educational :)

On Sat, Mar 29, 2008 at 6:38 PM, Sudara <markswilli...@gmail.com> wrote:

>  http://pastie.caboo.se/172487

>  Now, I'm sure this isn't the most beautiful way of going about things,
>  but it works, giving priority to matching against the source element
>  before checking parents.

This is an interesting point.  I'll see what the implications might
be.  I think the times when it becomes an issue are really infrequent
but as you say could be a pain.

Cheers,

--
Dan Webb
http://www.danwebb.net

aim: danwrong123
skype: danwrong


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sudara  
View profile
 More options 29 Mar, 20:28
From: Sudara <markswilli...@gmail.com>
Date: Sat, 29 Mar 2008 13:28:50 -0700 (PDT)
Local: Sat 29 Mar 2008 20:28
Subject: Re: delegating in jquery: problems
Wow, congats on your first son!

Indeed, stepping through the low pro code for jquery was
enlightening.

Before, the code was a grey mass of WTF. Now, I understand where and
how everything is happening. Still working on the why, your
metaprogramming chops go beyond my comprehension.

Sudara

On 29 Mar, 20:30, "Dan Webb" <d...@danwebb.net> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
lasitha  
View profile
 More options 26 Aug, 06:36
From: lasitha <lasitha.ranatu...@gmail.com>
Date: Tue, 26 Aug 2008 11:06:14 +0530
Local: Tues 26 Aug 2008 06:36
Subject: Re: delegating in jquery: problems
On Mar 27, 10:30 pm, Sudara <markswilli...@gmail.com> wrote:

> [...]

> Track = $.klass({
>   onclick: $.delegate({
>     '.track_link' : function(e){ this.toggleOptions(e) },
>     '.play_link' : function(e){ this.togglePlay(e) }
>   })

> [...]

> 3. For some odd reason, I can only match against 1 selector. [...]
> basically, whichever comes first.

Apologies for digging up an old post but i believe this bug (Item #3
in the OP) still exists in both Dan's and Sudara's branches.  In
Sudara's fork the bug only shows up if none of the selectors match the
event target itself _and_ the parent selector we want matched isn't
the first in the ruleset.

I've pastied patches against both Dan and Sudara's HEADs respectively:
http://pastie.org/260004
http://pastie.org/260009

Dan: belated congratulations on your firstborn!  Hope that's going
great and you still 'recommend this reproducing lark' :).

Cheers all.
lasitha.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google