You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by David Durham <dd...@vailsys.com> on 2005/08/19 23:59:16 UTC

[Friday] [somewhat-ajax-related] XMLHttpRequest scoping problems

Hi --

There's been some traffic on this list involving XMLHttpRequests and 
javascript, and since it's Friday ...

My problem: How to use multiple asynchronous requests simultaneously 
without using the global namespace.  For instance, I'd like to create a 
little DirectoryTree widget that I can drop in pages like so:

         <script type="text/javascript">
             new DirectoryTree("some_directory_name");
         </script>	

Ideally, I could drop as many of these widgets on the page as I want.

Then I have some javascript like so:

         DirectoryTree = function(directory, url) {
             //setup
             ...
             execute(url, DefaultCallback);
         }

Here's an execute method that supposed to trigger some kind of action:

         execute(url, callback) {
             var cb = new callback();
             cb.request = new XMLHttpRequest();
             cb.request.onreadystatechange = callback.processResponse;
             cb.request.open(this.METHOD, url, this.ASYNC);
             cb.request.send(null);
         }

         DefaultCallback = function() {
             this.request = null;

             function processResponse() {
                 log.info("decorating: " + this.request);
             }
         }


The processResponse() method alerts:  "decorating: undefined".  I even 
tried:

         DefaultCallback = function() {
             this.request = null;
	}

         DefaultCallback.processResponse = function() {
             log.info("decorating: " + this.request);
         }

and got the same result.

I'm somewhat experienced with Javascript and it's OO concepts and event 
handling, but not terribly so.  I think what's happening here is that on 
readystate change triggers the creation of a new 
'DefaultCallback.processResponse' object and therefore a new scope with 
no concept of the 'var cb = new callback()' scope.  Therefore, in order 
to get at the xmlrequest/response, I need to go through the global 
namespace or some other static-like namespace, which I don't really want 
to do because I'd like to avoid managing, say a queue of xml http 
requests.  I haven't looked through the xml http request stuff that was 
posted to struts sandbox (I think it's in sandbox, not really sure), but 
does anyone have ideas on this one, or can point me to some code or info 
with solution(s)?


- Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [Friday] [somewhat-ajax-related] XMLHttpRequest scoping problems

Posted by David Durham <dd...@vailsys.com>.
Jeff Beal wrote:
>   req.onreadystatechange = function() { processResponse(req); }

yeah, I think this is what I was looking for.  Thanks.


- Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [Friday] [somewhat-ajax-related] XMLHttpRequest scoping problems

Posted by Jeff Beal <jb...@gmail.com>.
I haven't actually tried having multiple simultaneous requests with
this, but it's not using the global namespace, so it should work:

submitRequest("../yourURL.html",processResponse);

function submitRequest(url,handler) {
  var req = null;
  if (window.XMLHttpRequest) { // Non-IE browsers
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // IE
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  req.onreadystatechange = function() { processResponse(req); }
  req.open("GET",url,true)
  req.send();
}

function processResponse(asyncRequest) {
   if (asyncRequest.readyState == 4 && asyncRequest.status == 200) {
      alert(asyncRequest.responseText);
   }
}

On 8/19/05, David Durham <dd...@vailsys.com> wrote:
> Hi --
> 
> There's been some traffic on this list involving XMLHttpRequests and
> javascript, and since it's Friday ...
> 
> My problem: How to use multiple asynchronous requests simultaneously
> without using the global namespace.  For instance, I'd like to create a
> little DirectoryTree widget that I can drop in pages like so:
> 
>          <script type="text/javascript">
>              new DirectoryTree("some_directory_name");
>          </script>
> 
> Ideally, I could drop as many of these widgets on the page as I want.
> 
> Then I have some javascript like so:
> 
>          DirectoryTree = function(directory, url) {
>              //setup
>              ...
>              execute(url, DefaultCallback);
>          }
> 
> Here's an execute method that supposed to trigger some kind of action:
> 
>          execute(url, callback) {
>              var cb = new callback();
>              cb.request = new XMLHttpRequest();
>              cb.request.onreadystatechange = callback.processResponse;
>              cb.request.open(this.METHOD, url, this.ASYNC);
>              cb.request.send(null);
>          }
> 
>          DefaultCallback = function() {
>              this.request = null;
> 
>              function processResponse() {
>                  log.info("decorating: " + this.request);
>              }
>          }
> 
> 
> The processResponse() method alerts:  "decorating: undefined".  I even
> tried:
> 
>          DefaultCallback = function() {
>              this.request = null;
>         }
> 
>          DefaultCallback.processResponse = function() {
>              log.info("decorating: " + this.request);
>          }
> 
> and got the same result.
> 
> I'm somewhat experienced with Javascript and it's OO concepts and event
> handling, but not terribly so.  I think what's happening here is that on
> readystate change triggers the creation of a new
> 'DefaultCallback.processResponse' object and therefore a new scope with
> no concept of the 'var cb = new callback()' scope.  Therefore, in order
> to get at the xmlrequest/response, I need to go through the global
> namespace or some other static-like namespace, which I don't really want
> to do because I'd like to avoid managing, say a queue of xml http
> requests.  I haven't looked through the xml http request stuff that was
> posted to struts sandbox (I think it's in sandbox, not really sure), but
> does anyone have ideas on this one, or can point me to some code or info
> with solution(s)?
> 
> 
> - Dave
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [Friday] [somewhat-ajax-related] XMLHttpRequest scoping problems

Posted by David Durham <dd...@vailsys.com>.
Sudhaker Raj wrote:
> You may want to check OpenRICO - http://openrico.org

Good link.  Much appreciated.


- Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [Friday] [somewhat-ajax-related] XMLHttpRequest scoping problems

Posted by Sudhaker Raj <su...@gmail.com>.
You may want to check OpenRICO - http://openrico.org

Cheers,

On 8/19/05, David Durham <dd...@vailsys.com> wrote:
> Hi --
> 
> There's been some traffic on this list involving XMLHttpRequests and
> javascript, and since it's Friday ...
> 
> My problem: How to use multiple asynchronous requests simultaneously
> without using the global namespace.  For instance, I'd like to create a
> little DirectoryTree widget that I can drop in pages like so:
> 
>          <script type="text/javascript">
>              new DirectoryTree("some_directory_name");
>          </script>
> 
> Ideally, I could drop as many of these widgets on the page as I want.
> 
> Then I have some javascript like so:
> 
>          DirectoryTree = function(directory, url) {
>              //setup
>              ...
>              execute(url, DefaultCallback);
>          }
> 
> Here's an execute method that supposed to trigger some kind of action:
> 
>          execute(url, callback) {
>              var cb = new callback();
>              cb.request = new XMLHttpRequest();
>              cb.request.onreadystatechange = callback.processResponse;
>              cb.request.open(this.METHOD, url, this.ASYNC);
>              cb.request.send(null);
>          }
> 
>          DefaultCallback = function() {
>              this.request = null;
> 
>              function processResponse() {
>                  log.info("decorating: " + this.request);
>              }
>          }
> 
> 
> The processResponse() method alerts:  "decorating: undefined".  I even
> tried:
> 
>          DefaultCallback = function() {
>              this.request = null;
>         }
> 
>          DefaultCallback.processResponse = function() {
>              log.info("decorating: " + this.request);
>          }
> 
> and got the same result.
> 
> I'm somewhat experienced with Javascript and it's OO concepts and event
> handling, but not terribly so.  I think what's happening here is that on
> readystate change triggers the creation of a new
> 'DefaultCallback.processResponse' object and therefore a new scope with
> no concept of the 'var cb = new callback()' scope.  Therefore, in order
> to get at the xmlrequest/response, I need to go through the global
> namespace or some other static-like namespace, which I don't really want
> to do because I'd like to avoid managing, say a queue of xml http
> requests.  I haven't looked through the xml http request stuff that was
> posted to struts sandbox (I think it's in sandbox, not really sure), but
> does anyone have ideas on this one, or can point me to some code or info
> with solution(s)?
> 
> 
> - Dave
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org