Michael Sharman
2008-Mar-10 06:10 UTC
Passing extra parameters to functions called from Observers
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
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Nikodim Lazarov
2008-Mar-10 06:39 UTC
Re: Passing extra parameters to functions called from Observers
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
<sharmo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
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-/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
-~----------~----~----~----~------~----~------~--~---
Michael Sharman
2008-Mar-10 06:46 UTC
Re: Passing extra parameters to functions called from Observers
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"
<nlaza...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
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
<sha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> 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-/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
-~----------~----~----~----~------~----~------~--~---
T.J. Crowder
2008-Mar-10 10:06 UTC
Re: Passing extra parameters to functions called from Observers
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
<sha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
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"
<nlaza...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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
<sha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> > 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-/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
-~----------~----~----~----~------~----~------~--~---
Ken Snyder
2008-Mar-10 15:10 UTC
Re: Passing extra parameters to functions called from Observers
Michael Sharman 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 >Perhaps you are looking for passing extra parameters to bindAsEventListener? function validateForm(event, param1, param2) {...} $(''myForm'').observe(''submit'', validateForm.bindAsEventListener(null, param1, param2)); - Ken Snyder --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Michael Sharman
2008-Mar-11 00:48 UTC
Re: Passing extra parameters to functions called from Observers
@T.J. - Gold! Thanks for taking the time, I owe you a beer :) Michael On Mar 11, 2:10 am, Ken Snyder <kendsny...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Michael Sharman 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 > > Perhaps you are looking for passing extra parameters to bindAsEventListener? > > function validateForm(event, param1, param2) {...} > > $(''myForm'').observe(''submit'', > validateForm.bindAsEventListener(null, param1, param2)); > > - Ken Snyder--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---