Forum: Rails Spinoffs (closed, excessive spam) check if item in siblings

Posted by louis w (Guest)
on 2008-06-20 00:38
(Received via mailing list)
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?
Posted by Dan Dorman (Guest)
on 2008-06-20 00:41
(Received via mailing list)
On Thu, Jun 19, 2008 at 4:35 PM, louis w <louiswalch@gmail.com> wrote:
>
> 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?

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

:Dan
Posted by Dan Dorman (Guest)
on 2008-06-20 00:53
(Received via mailing list)
On Thu, Jun 19, 2008 at 4:41 PM, Dan Dorman <dan.dorman@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
Posted by kangax (Guest)
on 2008-06-20 00:58
(Received via mailing list)
Dan,
not sure what Array#match is, but #grep could do this nicely : )

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

- kangax
Posted by Dan Dorman (Guest)
on 2008-06-20 01:14
(Received via mailing list)
On Thu, Jun 19, 2008 at 4:56 PM, kangax <kangax@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
Posted by kangax (Guest)
on 2008-06-20 03:36
(Received via mailing list)
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
Posted by Dan Dorman (Guest)
on 2008-06-20 17:47
(Received via mailing list)
On Thu, Jun 19, 2008 at 7:36 PM, kangax <kangax@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
This topic is locked and can not be replied to.