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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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 <jamesfiltn...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
I still get bit by this every once in a while :) - JDD --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
HI, Thanks for the replies. I don''t think i can use .each in this instance. droppableZones is being fed to my function right at the top and when i try and use each i get droppableZones.each is not a function error. See the code below. Also, if i just use a normal for loop, as below, i never get in to the inner function...i never get the alert(dropZoneCount) in the code below back. I need to keep the dropZoneCount so i can tell which dropzone the drop has been made in. Any ideas? function dragAndDrop(droppableZones) { var count = 0; var alldrags = $$(''.dragMe''); var alldragslength = alldrags.length; alldrags.each(function(w) { new Draggable(w, { zindex: false, revert: true }); }) for(var dropZoneCount = 1; dropZoneCount < droppableZones; ++ dropZoneCount) { Droppables.add(''droppable_'' + dropZoneCount, { accept: ''dragMe'', hoverclass: ''dropallowed'', onDrop : (function(i) { return function() { alert(dropZoneCount); count ++; var newCopy = new Element(''div'', { ''id'' : i.id, ''class'' : ''de_small'' }).update(''this is a copied element''); $(''droppable_'' + dropZoneCount).appendChild(newCopy); obj.remove(); if(count == alldragslength) { evaluate(); } } }) }) } } On May 19, 1:49 pm, jdalton <John.David.Dal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I still get bit by this every once in a while :) > > - JDD--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Dont know if you can do this here but here goes: BUMP! I still canne get this working On May 19, 9:43 pm, elduderino <jamesfiltn...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> HI, > > Thanks for the replies. I don''t think i can use .each in this > instance. droppableZones is being fed to my function right at the top > and when i try and use each i get droppableZones.each is not a > function error. See the code below. > > Also, if i just use a normal for loop, as below, i never get in to > the inner function...i never get the alert(dropZoneCount) in the code > below back. I need to keep the dropZoneCount so i can tell which > dropzone the drop has been made in. > > Any ideas? > > function dragAndDrop(droppableZones) { > var count = 0; > var alldrags = $$(''.dragMe''); > var alldragslength = alldrags.length; > alldrags.each(function(w) { > new Draggable(w, { zindex: false, revert: > true }); > }) > > for(var dropZoneCount = 1; dropZoneCount < > droppableZones; ++ dropZoneCount) { > Droppables.add(''droppable_'' + dropZoneCount, > { accept: ''dragMe'', hoverclass: ''dropallowed'', onDrop : (function(i) { > > return function() { > alert(dropZoneCount); > count ++; > var newCopy = new Element(''div'', { ''id'' : > i.id, ''class'' : ''de_small'' }).update(''this is a copied element''); > $(''droppable_'' + > dropZoneCount).appendChild(newCopy); > obj.remove(); > if(count == alldragslength) { > evaluate(); > } > } > }) > }) > } > } > > On May 19, 1:49 pm, jdalton <John.David.Dal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I still get bit by this every once in a while :) > > > - JDD--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---