Hi,

> In another situation, I need to pass parameters to the callback. So
> would I do it this way?
>
> var temp = handleCompletion(5)
> wrapAjaxUpdater('showsomething.php?id=5', 'somediv', temp);

You're calling the function immediately again.  This line:

> var temp = handleCompletion(5)

...calls the function handleCompletion with the argument 5 and assigns
the result of the function to the variable temp.  That's probably not
what you meant to do! :-)

What you want is a reference for a function that, when called, will
call handleCompletion with the value 5.  The generic way to do that is
to use your own function for it:

    wrapAjaxUpdater('showsomething.php?id=5', 'somediv', function() {
        handleCompletion(5);
    });

But this is such a common thing to want to do that Prototype provides
a mechanism for doing it:  Function#curry[1].  (I have no idea who
thought "curry" was a good name for this, but it's not the Prototype
developers' fault, they just used a term that was already being used
elsewhere.)  Using Function#curry:

    var temp = handleCompletion.curry(5);
    wrapAjaxUpdater(
        'showsomething.php?id=5',
        'somediv',
        temp
    );

...or just:

    wrapAjaxUpdater(
        'showsomething.php?id=5',
        'somediv',
        handleCompletion.curry(5)
    );

If you want to do that with a function where you *also* want to set
its context (the "this" value that will be in effect when the function
is called), use Function#bind[2] instead:

    wrapAjaxUpdater(
        'showsomething.php?id=5',
        'somediv',
        handleCompletion.bind(desiredContext, 5)
    );

In addition to simplicity and convenience, using curry() and bind()
means you don't create a closure in the current context (you create a
closure in a different context, but it's a pretty thin one so the
overhead is minimal), which can be useful sometimes.  For more about
closures, I wrote up a couple of blog entires on them a while back.[3]
[4]

[1] http://prototypejs.org/api/function/curry
[2] http://prototypejs.org/api/function/bind
[3] http://blog.niftysnippets.org/2008/02/closures-are-not-complicated.html
[4] http://blog.niftysnippets.org/2008/03/closures-by-example.html

<offtopic><soapbox>
BTW, it's OT, but I get the impression you're not very experienced in
JavaScript yet, so:  FWIW , I noticed in your code examples that you
frequently leave off semicolons at the ends of lines, e.g.:

> var temp = handleCompletion(5)
> wrapAjaxUpdater('showsomething.php?id=5', 'somediv', temp);

(There are three missing from your earlier exFetch function as well.)
That really should be:

> var temp = handleCompletion(5);
> wrapAjaxUpdater('showsomething.php?id=5', 'somediv', temp);

(Note the semicolon at the end of the first line.)  It's perfectly
valid syntax to leave off that semicolon, it's part of the JavaScript
specification that the interpreter will automatically insert it for
you in places where it thinks you meant to have one.  But it's an
extremely bad idea and arguably the worst "feature" in JavaScript.
Never rely on semicolon insertion, *always* include them yourself.
Not only does it make your intent clearer to those who read your code,
but it means that if you use minifiers and such (and who doesn't?),
your code will still work.  Many minifiers will remove line breaks,
which means your code above would become:

> var temp = handleCompletion(5) wrapAjaxUpdater('showsomething.php?id=5', 
> 'somediv', temp);

...which is *not* valid syntax and will cause an error.  If the
semicolon were there:

> var temp = handleCompletion(5); wrapAjaxUpdater('showsomething.php?id=5', 
> 'somediv', temp);

It would still work.  Just FWIW.  Some minifiers are better at
preserving necessary line breaks than others, but barring writing a
full JavaScript parser (most of these are regex-based), they're going
to get it wrong some of the time...
</soapbox></offtopic> ;-)

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

On Apr 15, 2:26 pm, Diodeus <diod...@gmail.com> wrote:
> Yes, I understand what you've said. I was making it more convoluted
> than need be.
>
> wrapAjaxUpdater('showsomething.php', 'somediv', handleCompletion);  -
> works correctly
>
> wrapAjaxUpdater('showsomething.php', 'somediv', handleCompletion()); -
> does not work because I'm calling the function, not passing a
> reference to it.
>
> In another situation, I need to pass parameters to the callback. So
> would I do it this way?
>
> var temp = handleCompletion(5)
> wrapAjaxUpdater('showsomething.php?id=5', 'somediv', temp);
--~--~---------~--~----~------------~-------~--~----~
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