Hi Vasily,

Just throw your own exception object and catch it at the level you
want the break to occur.  Something like:

// Probably just define this once somewhere
var $break2 = {};

// Use it
try {
  $R(minX, maxX).each(function(x) {
    $R(minY, maxY).each(function(y) {
     ...
     if (someCondition)
      throw $break2;
    });

  });
} catch (e) {
  if (e !== $break2) {
    // It's a real exception, rethrow it
    throw e;
}

The problem is that each loop has to know what exception objects the
other loop is using, so it's not great from a code cleanliness POV.
Alternately, set a condition in the inner loop that the outer loop
checks for, so at least they only need to know about each other and
not about any loops that might get used by functions they call...

FWIW,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available


On Apr 23, 1:23 am, Vasily Ivanov <basil...@gmail.com> wrote:
> Hi,
>
> I have two nested "each" loops (possibly more in the future) and I need to
> exit multiple levels at once on some condition. I do it like this:
>
> $R(minX, maxX).each(function(x) {
>   $R(minY, maxY).each(function(y) {
>    ...
>    if (someCondition)
>     throw $break;
>   });
>   if (someCondition)
>    throw $break;
>
> });
>
> Is it possible to use something like this instead to avoid code duplication?
>
> loopStart:
> $R(minX, maxX).each(function(x) {
>   $R(minY, maxY).each(function(y) {
>    ...
>    if (someCondition)
>     throw $break(loopStart);
>   });
>
> });
>
> I'm using prototype 1.6.0.3.
>
> Thank you,
>   Vasily
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to