It's a common confusion.
Long story short, "index" is shared via closure for all handlers.
You could fix it by wrapping it in another closure (which lets you
sort of "freeze" the variable for each handler) or use #each which
eliminates the problem as well:

Droppables.add('droppable_' + dropZoneCount, {
  accept: 'dragMe',
  hoverclass: 'dropallowed',
  onDrop: (function(i) {
    return function(){ /* use "i" is guaranteed to be "unique" here
*/ }
  })(dropZoneCount);
})

More prototype'ish way of iteration (over arrays) is:

droppableZones.each(function(zone, index) {
  Droppables.add('droppable_' + dropZoneCount, {
    accept: 'dragMe',
    hoverclass: 'dropallowed',
    onDrop: function(){ /* "index" is available here as well */ }
  })
})

Best,
kangax

On May 18, 6:51 pm, elduderino <[EMAIL PROTECTED]> wrote:
> HI,
>
> I have the following code.
>
>  for(var dropZoneCount = 1; dropZoneCount <= droppableZones; ++
> dropZoneCount) {
>      alert(dropZoneCount);
>      Droppables.add('droppable_' + dropZoneCount, { accept: 'dragMe',
> hoverclass: 'dropallowed', onDrop           function(obj){
>                 //
>
> }
> })
>
> I want to pass dropZoneCount in to the function that runs onDrop...how
> do i do this? I've tried passing it in the functions constructor like
> so:
>
>     Droppables.add('droppable_' + dropZoneCount, { accept: 'dragMe',
> hoverclass: 'dropallowed', onDrop function(obj, dropZoneCount){
> //
>
> but that does not work.
>
> Any ideas?
>
> (as a side questio  n..i've used a for loop in the code above...is
> there anything more prototypish? )
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" group.
To post to this group, send email to rubyonrails-spinoffs@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to