check if item in siblings

0 views
Skip to first unread message

louis w

unread,
Jun 19, 2008, 6:35:29 PM6/19/08
to Ruby on Rails: Spinoffs
is there an easier way then looping through the result of
Element.siblings(); to check and see if an item has a sibling with a
certain classname?

Dan Dorman

unread,
Jun 19, 2008, 6:41:02 PM6/19/08
to rubyonrail...@googlegroups.com

Dunno if it's faster per se, but el.siblings().match('.className') looks nice :)

:Dan

Dan Dorman

unread,
Jun 19, 2008, 6:52:35 PM6/19/08
to rubyonrail...@googlegroups.com
On Thu, Jun 19, 2008 at 4:41 PM, Dan Dorman <dan.d...@gmail.com> wrote:
>
> Dunno if it's faster per se, but el.siblings().match('.className') looks nice :)

But, y'know, too bad it doesn't work. Sorry about that. You could try
chaining findAll after siblings, but that kinda what you're trying to
avoid, huh?

:Dan

kangax

unread,
Jun 19, 2008, 6:56:44 PM6/19/08
to Ruby on Rails: Spinoffs
Dan,
not sure what Array#match is, but #grep could do this nicely : )

$(someElement).siblings().grep(new Selector('.someClass'));

- kangax

On Jun 19, 6:41 pm, "Dan Dorman" <dan.dor...@gmail.com> wrote:

Dan Dorman

unread,
Jun 19, 2008, 7:13:34 PM6/19/08
to rubyonrail...@googlegroups.com
On Thu, Jun 19, 2008 at 4:56 PM, kangax <kan...@gmail.com> wrote:
>
> Dan,
> not sure what Array#match is, but #grep could do this nicely : )
>
> $(someElement).siblings().grep(new Selector('.someClass'));

Yep, I'm not sure what I was smoking exactly, but I think it had some
jQuery in it :)

By way of penance, some extra credit work. Louis, If you find yourself
needing this functionality a lot, you could always augment the
behavior of siblings using Function.wrap:

Element.addMethods({
siblings: Element.siblings.wrap(function(proceed, element, selector) {
var sibs = proceed.call(this, element);
return selector ?
sibs.grep(selector instanceof Selector ?
selector :
new Selector(selector)) :
sibs;
})
});

:Dan

kangax

unread,
Jun 19, 2008, 9:36:01 PM6/19/08
to Ruby on Rails: Spinoffs
I actually find certain traversal methods too limiting. We could
easily make siblings/ancestors/etc. accept an optional selector/
iterator argument (or even both, where function would be treated as
iterator and string as selector). This also leads to a better
performance. I cooked a patch for this some time ago
http://dev.rubyonrails.org/ticket/11143 Does this make sense?

- kangax

Dan Dorman

unread,
Jun 20, 2008, 11:46:38 AM6/20/08
to rubyonrail...@googlegroups.com
On Thu, Jun 19, 2008 at 7:36 PM, kangax <kan...@gmail.com> wrote:
>
> I actually find certain traversal methods too limiting. We could
> easily make siblings/ancestors/etc. accept an optional selector/
> iterator argument (or even both, where function would be treated as
> iterator and string as selector). This also leads to a better
> performance. I cooked a patch for this some time ago
> http://dev.rubyonrails.org/ticket/11143 Does this make sense?

It makes a ton of sense, since before I even started work on that last
snippet, I was wondering why that sort of functionality wasn't already
in Prototype. That ancestors, siblings, et al, inherit from the same
method would seem to make it exceptionally low-hanging fruit. Any idea
on whether your patch will make it into the next release?

:Dan

Reply all
Reply to author
Forward
0 new messages