Home | Mail | Syndicate

welcome to my space

14 March, 2010 |

Problem with Ex.Ajax - callbacks are not assigned to proper object

  • Hi Folks ...

    this is my first post and first question, so hello to You all first

    Let's say I am a newbie to Ext and to JavaScript at all, but I have some programming experience with C++ and Java; so I am no absolute beginner, but relatively new to prototype based object oriented programming style.

    So up to my Problem:

    I try to create a browser running application style Rich Client platform in JavaScript, backend by a Apache/Tomcat/MySQL system. To achieve my result, I want to rely heavily on Ajax functionality; especially I want to use a kind of self designed RPC-Protocol.

    So I set up some JS-Objects encapsulating my RPC functions in a style, that a simple RPC call can be made by sending an Ext.Ajax.request to my server; method = POST, params = (a JSON object including the name of the called server-side function and the parameters to this function, as long with a unique message ID that is delivered back by server plainly for identify reasons) and, of course, callback = (a callback function to be handled by my RPC framework).

    Now, let's assume following course (simplified, because in reality it is much more code):


    RPCCall = function(callName, callBack, scope)
    {

    var cbObj = new Object();
    cbObj.callName = callName;
    cbObj.callBack = callBack;
    cbObj.scope = scope;
    cbObj.msgID = generate_unique_ID();

    var cbFunc = function(options, success, response){
    // do some validation

    // compare msgID from response with thst one from cbObj:
    if(!cbObj.msgID == response.msgID)
    {
    // here it is, where I realized that something went wrong
    }

    // if all ok:
    cbObj.callBack.call(cbObj.scope, response);
    }


    Ext.Ajax.request({

    callback: cbFunc,
    params: {callName: cbObj.callName, msgID: cbObj.msgID},
    scope: this

    });

    }

    and here some objects that will use RPCCall:


    Foo = function(serverFuncName){
    var myCallback = function(response)
    {
    // do something here
    }
    // fire the call immediately after creation:
    var myCall = new RPCCall(serverFuncName, myCallback, this);
    }
    So far, so good; that is my primary logic, so to say :-)

    So, I went on to test my RPCCall with some objects, like this:



    // first call ...
    var fastCall = new Foo(SERVER_FUNC_THAT_RETURNS_QUITE_FAST);
    // ... and directly after this, the second call:
    var slowCall = new Foo(SERVER_FUNC_THAT_TAKES_RELATIVELY_LONG);



    Now, I would expect, that the Ajax-response from the first call will return first and that one from the second call will return afterwards.
    Especially, I would expect that the callback function FROM THE RPCCall-OBJECT CREATED FIRSTLY will be called first.
    However, what I see is that indeed the http-response that returns firstly is the one from the "RELATIVELY_LONG" procedure - but it is called upon the SECOND object.

    I can relatively easily verify this, as I can compare the returned msgID from server with the one that was sent.

    What am I doing wrong here?

    I would be thankful for every hint :-)


    greetings from Germany

    Bomb


  • It looks like your scheme should work.

    Of course you don't need the scope option to Ext.Ajax.request because your callback is a closure which is bound to the scope in which the variables it uses were defined. It does not need a "this" - indeed, it does not use "this".

    What exactly is the problem that you are seeing? And do you examine the HTTP traffic in Firebug to see what was sent and recieved in each request/response transaction?


  • Yes, I am constantly stepping with Firebug through the critical callback functions, comparing my own variables.
    That is the reason, I realized something went wrong: the genuinely generated unique message ID that is sent to server from Object A is not the same in the callback function, so I must be in Object B.

    I will do some further investigation with the HTTP monitoring in Firebug as you suggested and will post results as I have them.

    Greetings
    Bombasstard


  • Slightly OT, but if you are using Java on the server, you might consider DWR which allow you to make transparent RPC calls directly to Java methods through a Javascript interface with the same signtaure. DWR exports the interface of a Java object to the browser.

    We use that where I work, for a lot of browser-server interactions.

    http://getahead.org/dwr

    There's a mailing list, but quite a few people here use it too, so you might get some help from here (I might even find time to help ;))


  • Hi Animal ...

    thanks for the quick reply.=D>
    I will have a look at the DWR thingy; that sounds interesting. If I would be able to integrate it into our (quite strangely orchestrated) software architecture, it would seem to be a lot of help.

    But meanwhile it would be nice, if someone would have a look at my code, respectively at the object/function/callback structure.

    I am desperately seeking the cause, why this fails. It may be a bug in Ext.lib.Ajax (I had a quick glance into the depth of the code in this matter, but do not claim to have it understood in all detail :">), so it would be interesting if someone else had a similar problem.

    Of course, as a JS newbie I think the error is on my side and would be grateful if someone would give me a hint what I'm doing wrong.

    greetings
    Bomb







  • #If you have any other info about this subject , Please add it free.#
    Your name:
    E-mail:
    Telphone:

    Your comments:


    If you have any other info about Problem with Ex.Ajax - callbacks are not assigned to proper object , Please add it free.
    PNNL, SGI Think Big Danes Launch Europe's First 24-Hour Internet TV Station