Editor's ajax function: I have Asynchronous issues

Editor's ajax function: I have Asynchronous issues

raydlevel5raydlevel5 Posts: 48Questions: 20Answers: 1

@allen I'm talking my boss into buying a copy. So, all of your help has NOT gone to waste.

I wish I weren't so dense, but I'm struggling: my problem is the 'A' in AJAX. Asynchronous. OR, getting my server's response INTO the success function.

Long story short: I can't use the built-in AJAX calls -- I have to use custom calls to the SQL server. We've established that I should be using Editor's ajax interceptor function:

ajax: function (method, url, data, successCB, errorCB) { /*CRUD stuff*/ }

I also understand that at the end of this function, I need to call the success callback with an object that tells Editor what just happened:

var output = { data: [] };  //data that Editor expects
successCB(output)

I call my app's special functions to create the new row:

                                if (data.action === 'create') {
                                    myApp.doRequest("createSQLwithMyJSON", aJSONstring);
                                }

Then, when the data is returned from SQL, it's isolate that that area of the code:

//AFTER SQL is done, client side, on the return:
switch (ActionRequested) {

     case "deleteSQLwithMyJson"
     //do a delete     
     break;

     case "editSQLwithMyJson"
     //do an edit
     break;

     case "createSQLwithMyJson"
     
     //get the data returned from the server
     str = $(data.xmldata).find("JSON").text();

     //create an object for Editor
     outputForDataTablesEditor = JSON.parse(str);

     /*unfortunately, {outputForDataTablesEditor} is stuck here, and I can't reference outside of this block*/
     break;


}

How do I pass {outputForDataTablesEditor} into successCB?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 47,045Questions: 1Answers: 6,628 Site admin

    From your post is sounds like you are calling successCB too early. You should do it after you have loaded the data from the server (outputForDataTablesEditor). What is in that variable (i.e. what is the JSON structure)?

    Allan

  • raydlevel5raydlevel5 Posts: 48Questions: 20Answers: 1

    @allan {outputForDataTablesEditor} will be a response for SQL. It'll be a JSON object with the structure that Editor expects.

    I need to get this object into the success callback somehow.

    This pseudo-code might be better than what I previously supplied:

    //EDITOR
    ajax: function (method, url, data, successCB, errorCB) {
        if (data.action === 'create') {
            myApp.doAction("createSQLwithMyJSON", aJSONstring);
        }
    
    
        successCB(outputForDataTablesEditor)
    
    }
    
    
    //APPLICATION RESPONSE HANDLER
    function doResult(returnedData) {
    
        switch (ActionRequested) {
    
    
            case "createSQLwithMyJson"
    
                //server response
                str = $(data.xmldata).find("JSON").text();
    
                //create an object for Editor success callback
                outputForDataTablesEditor = JSON.parse(str);
    
                //give Editor the expected response here;
                //call the callback ???
    
                break;
    
        }
    }
    
    
  • allanallan Posts: 47,045Questions: 1Answers: 6,628 Site admin

    Does your doAction function have the option of a callback that will be executed when the doResult for that action is finished? Or can you pass parameters to it that you can use for execution?

    Allan

  • raydlevel5raydlevel5 Posts: 48Questions: 20Answers: 1

    @allan Yes, they both can receive parameters... (I think we can do callbacks too, but I know for sure we can do params)

    Additionally, regardless of our framework, is there away to call sucessCB from _outside _of Editor??

  • allanallan Posts: 47,045Questions: 1Answers: 6,628 Site admin
    Answer ✓

    No - you need to pass it through the chain since you need a reference to that callback function (its got closure values inside it relating to the request that was made).

    If you aren't able to pass the callback through (for example doAction( "createSQLwithMyJSON", aJSONstring, successCB); which is the typical way of doing it in Javascript) then you would have to assign successCB as a global variable which both functions can access. I would urge caution if you take that approach since globals can make maintenance really difficult over the longer term.

    The ideal solution is to be able to pass the callback through to doResult, but how that is done is down to the framework you are using.

    Allan

  • raydlevel5raydlevel5 Posts: 48Questions: 20Answers: 1

    @allan Thank you so much. This really made me happy today -- and I learned something! I didn't know that you could send an entire physical function as a parameter.

    I was able to pass along the {successCB} into doResult.

    //from within Editor's AJAX interceptor function,
    doRequest("editorCreateFNC", form, successCB);
    
    
    
    //doRequest carries the successCB to doResults
    doRequest: function (action, form, options)
    
    
    
    doResults: function (data, options)
    
            case "editorCreateFNC":
    
    
            str = $(data.xmldata).find("JSON").text();
            doME = JSON.parse(str);
    
            theActualSuccessCB = options
            theActualSuccessCB(doME);
    
  • allanallan Posts: 47,045Questions: 1Answers: 6,628 Site admin

    Yeah, functions as first class objects is one of the main reasons I love Javascript as much as I do :).

    Allan

Sign In or Register to comment.