SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Apr 2004
    Location
    chicago
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    callback functions that return a value

    I've spent way too long trying to figure this out, hopefully an expert here can give me some advice...

    basically I have a class like this:

    http = ajax xmlHttpRequest object, this works fine
    function obj()
    {
    this.returnval = null;
    this.getfunction = getfunction;
    }

    function getfunction(id)
    {
    http.open("GET", "get.php" + id, true);
    http.onreadystatechange = _getfunction;
    http.send(null)
    return this.returnval;
    }

    function _getfunction()
    {
    if(http.readyState == 4)
    {
    // process returned xml from get.php
    // and load it in object obj
    this.returnval = obj;
    }
    }

    so there's the code - problem is, this.returnval is always null. I've confirmed that obj is loaded with actual data, and exists as it should in _getfunction, but for some reason getfunction still has this.returnval = null

    I've tried fiddling with it, using a global variable, and none of it worked. it's driving me crazy - I just want the xmlrequestobject callback function to be able to return a value to the function one level up...any help?

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure why you need to use a class, but you can't reference this.returnval procedurally after calling send. It doesn't pause at the send line while it waits for a response. The next action that will reference this.returnval needs to be in the function you set in onreadystatechange

  3. #3
    SitePoint Member
    Join Date
    Apr 2004
    Location
    chicago
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm using classes because I want to encapsulate various components to keep it clean - the ajax component shouldn't access the interface components directly. is there any way for an object created in the callback to be available outside? e.g. once the request is completed, return it from the class to the calling function?

  4. #4
    SitePoint Addict dek's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I feel you need to change the way you're thinking about this. As torunforever said, you can't do this procedurally - the request returns asynchronously, which means that you don't know how long it will take.

    When you send a request, you're basically handing over control to the request object, and control is given back to your code by said object when the request returns.

    I can come up with an off-the-cuff example if you like, but it'd probably be better if you had a look at the many AJAX implementations that are out there - you should easily find something that you can adapt to your liking.
    Only dead fish go with the flow

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dek explained the asynchronously aspect better than I did. Plus I probably confused things by saying "I'm not sure why you need to use a class". There's nothing wrong with encapsulating functionality.

    is there any way for an object created in the callback to be available outside? e.g. once the request is completed, return it from the class to the calling function?
    If you read enough examples, you'll probably find that you can solve your overall problem without the need to "return it from the class to the calling function".

    You obviously have the basics down, but in case you missed this one, check out:
    http://developer.apple.com/internet/...mlhttpreq.html

    And you can find lots of ajax information at:
    http://www.ajaxian.com/

  6. #6
    SitePoint Member
    Join Date
    Apr 2004
    Location
    chicago
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your responses, I think it does have to do with how I'm thinking about the problem. I'm looking for more examples to learn from, but if I could describe the simple (maybe not optimal) solution I came up with (I'm still looking for not-too-beginner yet not-too-advanced examples for other ideas)

    - I use a global to access the data returned to the request handler (I hate to use globals though, but maybe it's not so against the rules in JS?)
    - the class that makes the request to the ajax engine doesn't process the global's value right away - this was my mistaken assumption
    - instead, it sets a timer to check every 50ms if the global is non-null
    - this way, I can handle the event of a server timeout

    this seems to work, but before I go too far with a timer system I wanted to see if anyone saw any pitfalls with this method. it's just a small hobby project now, but I've been out of the loop on web programming for a bit so it's tough to not get frustrated :P

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been reading more about XMLHttpRequest. I'm only just beginning with it, so it was a little presumptuous of me to be offering advice.

    I'd like to correct something I said before. It is possible to make the request synchronously. However, this isn't advised. For an explanation, refer to:
    http://www.sitepoint.com/article/remote-scripting-ajax

    The open() method also takes an optional third boolean argument that specifies whether the request is made asynchronously (true, the default) or synchronously (false). With a synchronous request, the browser will freeze, disallowing any user interaction, until the object has completed. An asynchronous request occurs in the background, allowing other scripts to run and letting the user continue to access their browser. It's recommended that you use asynchronous requests; otherwise, we run the risk of a user's browser locking up while they wait for a request that went awry.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •