Enumerations and Invoke

23 views
Skip to first unread message

kstubs

unread,
May 5, 2012, 10:57:11 PM5/5/12
to prototype-s...@googlegroups.com
How do I implement:

I have a function
I have an enumeration
On the enumeration I want to invoke my function
My function should either receive the current enumerable object as first parameter - or - within the context of the function "this" should be the current enumerable item.

Suppose I have a collection of <div> elements.  I want to invoke my function so my function might look like:

function myFunction(div) {
  // div is the current enumerable object
}

I call it like this:
$$('div.mydivs').invoke('myFunction')

Thanks,
Karl..

Walter Lee Davis

unread,
May 5, 2012, 11:50:58 PM5/5/12
to prototype-s...@googlegroups.com
Take a look at methodize, or you can do this:

Element.addMethods({
myFunction: function(div){
var div = $(div);
//do whatever here
return div; //for chaining
}
});

Walter
> --
> You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/prototype-scriptaculous/-/v1D0luPFTN0J.
> To post to this group, send email to prototype-s...@googlegroups.com.
> To unsubscribe from this group, send email to prototype-scripta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.

kstubs

unread,
May 6, 2012, 12:33:37 AM5/6/12
to prototype-s...@googlegroups.com
Thanks!  This should work.

Brian Marquis

unread,
May 7, 2012, 10:21:21 AM5/7/12
to prototype-s...@googlegroups.com

You could also do this with each.

 

$$('div.mydivs').each(myFunction);

 

myFunction will be passed the current element and the index of that element.

 

Brian Marquis | Quotepro® | Senior Developer | bm@quotepro.com | Phone: 312.654.8045 x122 / Fax: 312.654.1285

image001

The information in this e-mail is confidential and may be legally privileged.  It is intended solely for the addressee.   Access to this e-mail by anyone else is unauthorized.

--

You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.

image001.png
Reply all
Reply to author
Forward
0 new messages