Parallele Ajax, parameters to response handler?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Loose

    Parallele Ajax, parameters to response handler?

    hi dear folks,

    i'm new to ajax. simple request no problem - but it seems when I set
    the response handler function, I can only pass the function name and
    not give parameters, so I have a problem when starting e.g. 5 ajax
    requests parallele. how to do that correctly? for one case, where I
    used *different* resp. handlers, I solved the problem by global
    variables - but now I have a loop of e.g. 20 calls to the *same*
    r.handler and I don*t know how to tell the function *which* request of
    the 20 to use. I expect the solution to be simple but how... nothing
    found on a quick google, sorry...

    thanx in advance! have a nice sunday and week, daniel
  • David Mark

    #2
    Re: Parallele Ajax, parameters to response handler?

    On Aug 12, 7:54 am, n...@email.de (Daniel Loose) wrote:
    hi dear folks,
    >
    i'm new to ajax. simple request no problem - but it seems when I set
    New to what? I assume you are using some Ajax library.
    the response handler function, I can only pass the function name and
    not give parameters, so I have a problem when starting e.g. 5 ajax
    requests parallele. how to do that correctly? for one case, where I
    used *different* resp. handlers, I solved the problem by global
    variables - but now I have a loop of e.g. 20 calls to the *same*
    r.handler and I don*t know how to tell the function *which* request of
    the 20 to use. I expect the solution to be simple but how... nothing
    You do it with anonymous functions and closures or you can use the
    Function constructor.

    Without seeing your original code, I can't tell you how to modify it.

    Comment

    • Daniel Loose

      #3
      Re: Parallele Ajax, parameters to response handler?

      On Sun, 12 Aug 2007 05:05:36 -0700, David Mark
      <dmark.cinsoft@ gmail.comwrote:

      Hi David, thank you for your fast reply.
      >On Aug 12, 7:54 am, n...@email.de (Daniel Loose) wrote:
      >hi dear folks,
      >>
      >i'm new to ajax. simple request no problem - but it seems when I set
      >
      >New to what? I assume you are using some Ajax library.
      New to Ajax. No I don't like libraries if not necessary. Actually I
      build my own, but that's another topic. I always wish to have/ use/
      understand the "original" code and objects... in the beginning.
      >
      >the response handler function, I can only pass the function name and
      >not give parameters, so I have a problem when starting e.g. 5 ajax
      >requests parallele. how to do that correctly? for one case, where I
      >used *different* resp. handlers, I solved the problem by global
      >variables - but now I have a loop of e.g. 20 calls to the *same*
      >r.handler and I don*t know how to tell the function *which* request of
      >the 20 to use. I expect the solution to be simple but how... nothing
      >
      >You do it with anonymous functions and closures or you can use the
      >Function constructor.
      >
      >Without seeing your original code, I can't tell you how to modify it.
      >
      ok it's just like [the requestId stuff is my workaround for calling
      different response handlers in parallele as mentioned]
      >>
      function wuwinoAjax (script, responseHandler , paraString, reqId) {

      requestId = (reqId) ? reqId : 'firstRequest' ;

      httpRequest[requestId] = false;

      if (window.XMLHttp Request) { // Mozilla, Safari,...

      httpRequest[requestId] = new XMLHttpRequest( );
      if (httpRequest[requestId].overrideMimeTy pe)
      httpRequest[requestId].overrideMimeTy pe('text/xml');

      } else if (window.ActiveX Object) { // IE

      try {
      httpRequest[requestId] = new
      ActiveXObject(" Msxml2.XMLHTTP" );
      } catch (e) {

      try {
      httpRequest[requestId] = new
      ActiveXObject(" Microsoft.XMLHT TP");
      } catch (e) {}
      }
      }

      if (!httpRequest[requestId]) { return false; }

      httpRequest[requestId].onreadystatech ange = responseHandler ;
      httpRequest[requestId].open('GET', rootURL +
      'wuwino/ajax.php?script =' + script + paraString, true);
      httpRequest[requestId].send(null);
      }

      httpRequest = new Array();
      requestToUse = new Array();
      //default
      requestToUse['updateTraittyp eBoxes'] = 'firstRequest';

      <<

      .... called with eg...
      >>
      requestToUse['updateTraittyp eBoxes'] = 'secondRequest' ;
      ajax('traittype s', updateTraittype Boxes, 'traittypeId=55 ',
      'secondRequest' );

      <<

      .... and the responseHandler does...
      >>
      function updateTraittype Boxes () {

      http_request =
      httpRequest[requestToUse['updateTraittyp eBoxes']];

      ....
      }

      <<

      Thanx again in advance!

      Comment

      • David Mark

        #4
        Re: Parallele Ajax, parameters to response handler?

        On Aug 12, 8:25 am, n...@email.de (Daniel Loose) wrote:
        On Sun, 12 Aug 2007 05:05:36 -0700, David Mark
        >
        <dmark.cins...@ gmail.comwrote:
        >
        Hi David, thank you for your fast reply.
        >
        On Aug 12, 7:54 am, n...@email.de (Daniel Loose) wrote:
        hi dear folks,
        >
        i'm new to ajax. simple request no problem - but it seems when I set
        >
        New to what? I assume you are using some Ajax library.
        >
        New to Ajax. No I don't like libraries if not necessary. Actually I
        build my own, but that's another topic. I always wish to have/ use/
        understand the "original" code and objects... in the beginning.
        >
        Good.

        [snip]
        >
        ok it's just like [the requestId stuff is my workaround for calling
        different response handlers in parallele as mentioned]
        >
        >
        >
        function wuwinoAjax (script, responseHandler , paraString, reqId) {
        >
        requestId = (reqId) ? reqId : 'firstRequest' ;
        >
        httpRequest[requestId] = false;
        >
        if (window.XMLHttp Request) { // Mozilla, Safari,...
        >
        IE7 also ends up in here, so the comment isn't accurate.
        httpRequest[requestId] = new XMLHttpRequest( );
        if (httpRequest[requestId].overrideMimeTy pe)
        httpRequest[requestId].overrideMimeTy pe('text/xml');
        Why are you overriding the MIME type? Is it not set properly on the
        server?
        >
        } else if (window.ActiveX Object) { // IE
        >
        try {
        httpRequest[requestId] = new
        ActiveXObject(" Msxml2.XMLHTTP" );
        } catch (e) {
        >
        try {
        httpRequest[requestId] = new
        ActiveXObject(" Microsoft.XMLHT TP");
        } catch (e) {}
        }
        }
        >
        if (!httpRequest[requestId]) { return false; }
        >
        httpRequest[requestId].onreadystatech ange = responseHandler ;
        httpRequest[requestId].open('GET', rootURL +
        'wuwino/ajax.php?script =' + script + paraString, true);
        httpRequest[requestId].send(null);
        >
        }
        >
        httpRequest = new Array();
        requestToUse = new Array();
        You are misusing arrays here. In this context, you should Object
        objects.
        //default
        requestToUse['updateTraittyp eBoxes'] = 'firstRequest';
        >
        <<
        >
        ... called with eg...
        >
        >
        >
        requestToUse['updateTraittyp eBoxes'] = 'secondRequest' ;
        ajax('traittype s', updateTraittype Boxes, 'traittypeId=55 ',
        'secondRequest' );
        >
        <<
        >
        ... and the responseHandler does...
        Actually this is a ready state handler.
        >
        >
        >
        function updateTraittype Boxes () {
        >
        http_request =
        httpRequest[requestToUse['updateTraittyp eBoxes']];
        >
        Since this function is called as a method of the request object, you
        can reduce this to:

        http_request = this;

        It doesn't appear that you need to keep track of anything else, so
        just delete the two arrays.

        Comment

        • Richard Cornford

          #5
          Re: Parallele Ajax, parameters to response handler?

          David Mark wrote:
          On Aug 12, 8:25 am, Daniel Loose wrote:
          <snip>
          >function updateTraittype Boxes () {
          >>
          > http_request =
          >httpRequest[requestToUse['updateTraittyp eBoxes']];
          >>
          >
          Since this function is called as a method of the request
          object, you
          can reduce this to:
          >
          http_request = this;
          <snip>

          At least some XML HTTP request object (ActiveX versions on windows IE)
          do not call their - onreadystatecha nge - handlers as method of the
          request object. That was not a good design decision, but it is why you
          almost never see example handlers trying to use - this - to reference
          the object.

          Richard.

          Comment

          • David Mark

            #6
            Re: Parallele Ajax, parameters to response handler?

            On Aug 12, 10:34 am, "Richard Cornford" <Rich...@litote s.demon.co.uk>
            wrote:
            David Mark wrote:
            On Aug 12, 8:25 am, Daniel Loose wrote:
            <snip>
            function updateTraittype Boxes () {
            >
            http_request =
            httpRequest[requestToUse['updateTraittyp eBoxes']];
            >
            Since this function is called as a method of the request
            object, you
            can reduce this to:
            >
            http_request = this;
            >
            <snip>
            >
            At least some XML HTTP request object (ActiveX versions on windows IE)
            do not call their - onreadystatecha nge - handlers as method of the
            request object. That was not a good design decision, but it is
            After I posted, it occurred to me that MS might have a monkey wrench
            in the works of that solution. I've never ran into it as my object
            for Ajax requests handles the readystatechang e events internally and
            passes a reference to the request object to external handlers.

            Comment

            • Martin Honnen

              #7
              Re: Parallele Ajax, parameters to response handler?

              Richard Cornford wrote:
              At least some XML HTTP request object (ActiveX versions on windows IE)
              do not call their - onreadystatecha nge - handlers as method of the
              request object. That was not a good design decision, but it is why you
              almost never see example handlers trying to use - this - to reference
              the object.
              Mozilla in released versions (including Firefox 2.0) makes the |this|
              object in the onreadystatecha nge handler the handler function itself.


              --

              Martin Honnen

              Comment

              • David Mark

                #8
                Re: Parallele Ajax, parameters to response handler?

                On Aug 12, 9:00 am, David Mark <dmark.cins...@ gmail.comwrote:
                On Aug 12, 8:25 am, n...@email.de (Daniel Loose) wrote:
                >
                >
                >
                >
                >
                On Sun, 12 Aug 2007 05:05:36 -0700, David Mark
                >
                <dmark.cins...@ gmail.comwrote:
                >
                Since this function is called as a method of the request object, you
                can reduce this to:
                >
                http_request = this;
                >
                As Richard pointed out, MS screwed up their implementation of Ajax
                event handling. What you need to do is define an inner function in
                wuwinoAjax and use it to handle readystatechang e events. Make your
                callback inside that function and pass a reference to the request
                object. As the request object (and the callback function) will be
                preserved in a closure that creates a circular reference, make sure
                you set the onreadystatecha nge property of the request object to null
                when the request finishes loading.

                Comment

                • dhtmlkitchen@gmail.com

                  #9
                  Re: Parallele Ajax, parameters to response handler?

                  On Aug 12, 4:54 am, n...@email.de (Daniel Loose) wrote:
                  hi dear folks,
                  >
                  i'm new to ajax. simple request no problem - but it seems when I set
                  the response handler function, I can only pass the function name and
                  not give parameters, so I have a problem when starting e.g. 5 ajax
                  requests parallele. how to do that correctly?
                  YUI recommends adding expando properties to the callback function
                  object. This works, but is horrible design.

                  // Non-explicit and error-prone approach.
                  myCallback.arg = "poop";

                  I recommend using a different approach. Not the uid approach -- an
                  observer-based approach.

                  The implementation would look something like:
                  var req = Ajax.createConn ection( url, "POST", true, reqId );
                  EventRegistry.a dd( req, Ajax.Events.SUC CESS, cbSuccessFuncti on );
                  req.send( aForm.getDataSe tString() );

                  function cbSuccessFuncti on( ajaxEvent ) {
                  var reqId = ajaxEvent.reqId ;
                  var userPanel = Panel.getById( reqId );
                  userPanel.setCo ntent( ajaxEvent.respo nseText );
                  userPanel.show( );
                  }

                  Create a custom event class that can handle this behavior and your
                  set.

                  Garrett


                  Comment

                  • Daniel Loose

                    #10
                    Re: Parallele Ajax, parameters to response handler?

                    On Sun, 12 Aug 2007 08:14:10 -0700, David Mark
                    <dmark.cinsoft@ gmail.comwrote:
                    >On Aug 12, 9:00 am, David Mark <dmark.cins...@ gmail.comwrote:
                    >On Aug 12, 8:25 am, n...@email.de (Daniel Loose) wrote:
                    >>
                    >What you need to do is define an inner function in
                    >wuwinoAjax and use it to handle readystatechang e events. Make your
                    >callback inside that function and pass a reference to the request
                    >object. As the request object (and the callback function) will be
                    >preserved in a closure that creates a circular reference, make sure
                    >you set the onreadystatecha nge property of the request object to null
                    >when the request finishes loading.
                    >
                    Thank you very much, and all posters! Sorry to ask again, but I'm
                    unexperienced in advanced javascript (often ending up losing hours to
                    code sth that's actually quite simple) - could you please give some
                    code example? Needs not be complete and working, just the main lines/
                    functionality would really help me out a lot. Thanx again and best
                    wishes to all!

                    Comment

                    • Ben Amada

                      #11
                      Re: Parallele Ajax, parameters to response handler?

                      David Mark wrote:
                      On Aug 12, 8:25 am, n...@email.de (Daniel Loose) wrote:
                      >>
                      > if (window.XMLHttp Request) { // Mozilla, Safari,...
                      IE7 also ends up in here, so the comment isn't accurate.
                      In most cases, IE7 would support XMLHttpRequest. But not always since
                      native XMLHTTP support can easily be turned off in IE7 on the Advanced tab
                      of the Internet Options dialog window.


                      Comment

                      Working...