Let's start with a simple example:

function alertData(data) {
  alert(data);
}

//named callback function
$("#mychild").ajaxForm({
  success: alertData
});

Notice I just give success the name of my function - alertData. No function
keyword, no parens, no curly braces. I'm actually passing the function
itself as a parameter. This is known as a callback. If it succeeds, ajaxForm
will call this function I provide, passing the data as the first (and only)
parameter. This is in contrast to our earlier example:

//anonymous callback function
$("#mychild").ajaxForm({
  success: function(data) {
    alert(data);
  }
});

That's called an anonymous function. The function doesn't have a name, so
you can't call it elsewhere. You're declaring it as you're passing it.
Beautiful, huh?

If you want to really study javascript, have you read John Resig's book?:

http://www.amazon.com/Pro-JavaScript-Techniques-John-Resig/dp/1590597273/

Highly recommended.

- Richard

On Dec 14, 2007 12:55 PM, Mario Moura <[EMAIL PROTECTED]> wrote:

>   Wow, thanks a lot Richard
>
> I can do this with php (quite simple) but I am lost with Jquery /
> Javascript syntax. Could you help me?
>
> Example
>
>      $("#mychild").ajaxForm({
>         success:
>             function(data) {
>                   // do stuff with data;
>                 function toCheck() {
>                     $(data);
>                 }
>             }
>         });
>
>         // So for each Ajax request I will insert
>             function toCheck() {
>                 $(data);  // or I can get some data into $data like
> $(data).attr("myid")
>             }
>
>         // So I want check if toCheck() have a value or not but I will let
> simple, just alert me.
>         // I dont know if the syntax is ok
>
>             function pleaseAlertMe() {
>                 alert(toCheck());
>             }
>
>             I dont want (or I cant) pass the $(data) to a div and check
> there by DOM I prefer check by function. If is it possible of course.
>
>             And now I will study a lot of JavaScript Syntax. ;)
>
> Regards
>
> Mario Moura
>
> 2007/12/14, Richard D. Worth < [EMAIL PROTECTED]>:
>
> > You could declare a function and give it a name, instead of using an
> > anonymous function, then pass that as the success function for each ajax
> > call.
> >
> > - Richard
> >
> > On Dec 14, 2007 8:51 AM, Mario Moura < [EMAIL PROTECTED]> wrote:
> >
> > > No.
> > >
> > > Let me explain better.
> > >
> > > In your scenario you made a individual AJAX request and into this
> > > request you create rules.
> > >
> > > But JQuery  could create an universal AjaxSucess to test all data from
> > > all ajax requests.
> > >
> > > So imagine this scenario:
> > >
> > > The page have a lot of AJAX requests, something like ajaxform, $.ajax,
> > > $.post, $.get
> > >
> > > So from response of each AJAX we could create a function like: (and I
> > > will use switch)
> > >
> > >
> > > $().ajaxSuccess (function() {
> > >          switch( $(data).attr("id")  ) { // here I get the data from
> > > AJAX Sucess every time happen could by by xml, json but I am using div
> > >             case 'myid':
> > >                 // do stuff with data;
> > >             break;
> > >             case 'myid2':
> > >                // do stuff with data;
> > >             break;
> > >          }
> > >
> > > no matter if it is from $.ajax({"type": "POST",  "url": " foo1.php",}); 
> > > or  $.ajax({"type": "POST", "url": "
> > > foo2.php "}); or $.ajax({"type": "POST", "url": "foo3.php ",});
> > >
> > > But at this moment. I think is not possible. But could be a good
> > > improve.
> > >
> > > Did you understand?
> > >
> > > Regards
> > >
> > > Mario Moura
> > >
> > > 2007/12/14, Giovanni Battista Lenoci < [EMAIL PROTECTED]>:
> > >
> > > >
> > > > Don't know if I understood,
> > > >
> > > >    $.ajax({"type": "POST",
> > > >       "url": " foo2.php",
> > > >       "data": data,
> > > >       "dataType": "json",
> > > >       "success": function(server_response) {
> > > >           switch(server_response.wich_function) {
> > > >              case '1':
> > > >                 on_success_one(server_response);
> > > >              break;
> > > >              case '2':
> > > >                 on_success_two(server_response);
> > > >              break;
> > > >           }
> > > >
> > > >       });
> > > >
> > > >
> > >
> > >
> > > --
> > > Mário Alberto Chaves Moura
> > > [EMAIL PROTECTED]
> > > 31-3264-6203
> > > 31-9157-6000
> >
> >
> >
>
>
> --
> Mário Alberto Chaves Moura
> [EMAIL PROTECTED]
> 31-3264-6203
> 31-9157-6000
>

Reply via email to