Hi Michael,

You still need the "event" param to your anonymous function, so it can
pass that to the validateForm function.  Here's a more complete
example (off the top of my head, not tested):

function doSomeSetup()
{
    var somethingValidateNeedsToKnow;

    // ... set somethingValidateNeedsToKnow to something that
    // validate needs to know

    // Set up the handler
    $('myForm').observe('submit', function(evt) {
        validateForm(evt, somethingValidateNeedsToKnow);
    });
}

This works because your anonymous function is a closure, so it retains
a reference to the variables and arguments in scope where it's
defined, even after the execution of that scope (e.g., the doSomeSetup
function) completes.  Consequently, when it's triggered by the event,
it still has a reference to the somethingValidateNeedsToKnow value in
the setup function. This is probably what Niko meant about being
careful what variables are in scope, etc. (More on closures here:
http://niftysnippets.blogspot.com/2008/02/closures-are-not-complicated.html.)

Hope this helps,
--
T.J. Crowder
tj / crowder software / com

On Mar 10, 6:46 am, Michael Sharman <[EMAIL PROTECTED]> wrote:
> Hi Niko,
>
> Thanks for the swift reply!
>
> By 'handler' did you mean doing something like:
>
> $('myForm').observe('submit', function(){validateForm(event,
> param1)});
>
> If so then that didn't work for me.
>
> Michael
>
> On Mar 10, 5:39 pm, "Nikodim Lazarov" <[EMAIL PROTECTED]> wrote:
>
> > Hi,
>
> > The answer to your question is yes. Still you need to change the handler
> > to this:
>
> > function(event){validateForm(event, param1, param2)}
>
> > and be very careful what happens with the variable scope, i.e. if the
> > parameters are visible in the current scope.
>
> > Best Regards,
> > Niko
>
> > On Mon, 10 Mar 2008 08:10:58 +0200, Michael Sharman <[EMAIL PROTECTED]>
> > wrote:
>
> > > Hi again,
>
> > > When you have an 'obeserver' setup as follows:
>
> > > $('myForm').observe('submit', validateForm);
>
> > > If a user submits the form the 'validateForm' function is called and
> > > is automagically passed the 'event' to work with. This is fantastic!
>
> > > But is there a way to pass another argument to the function? Something
> > > like:
>
> > > $('myForm').observe('submit', validateForm, param1, param2);
>
> > > etc?
>
> > > Michael
>
> > --
> > Best Regards,
> > Nikodim Lazarov
--~--~---------~--~----~------------~-------~--~----~
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