0

I am trying to call the success call back function inside the jsonGet method and trying to access the variable "size " and "noOfFiles" outside the JsonGet method but it is showing undefined outside the jsonGet method.

I am using the following code

(function()
{
/**
 * Alfresco Slingshot aliases
 */
var $html = Alfresco.util.encodeHTML,
 $isValueSet = Alfresco.util.isValueSet;

var $html5 = Alfresco.util.encodeHTML,
$isValueSet = Alfresco.util.isValueSet;


if (Alfresco.DocumentList)
{  YAHOO.Bubbling.fire("registerRenderer",
            {
                propertyName: "FolderInfo",

               renderer: function type_renderer(record)
               {
                    var jsNode = record.jsNode;  
                    var nodeRef = jsNode.nodeRef;
                    var size, noOfFiles;

                    console.log(size);
                    console.log(Alfresco.constants.PROXY_URI + "com/acme/nodesize/node-size.json?nodeRef=" + nodeRef);

                    /*Alfresco.util.Ajax.jsonGet({*/
                        $.ajax({
                        url: encodeURI(Alfresco.constants.PROXY_URI + 'com/acme/nodesize/node-size.json?nodeRef='+nodeRef),
                        async: false,
                        successCallback:
                        {
                           fn: function loadWebscript_successCallback(response, config)
                           {
                               var obj = JSON.parse(response.serverResponse.responseText);
                               if (obj)
                               {
                                 console.log("AJAX calls works. Printing the folder size " + obj.size + " & no of files " + obj.noOfFiles);
                                   size = obj.size;
                                   noOfFiles = obj.noOfFiles;
                                   alert(size);
                                   console.log("inside success call back function");
                               }
                           },
                           /*scope: this*/
                        },
                        /*scope: this*/
                     });

                        console.log("Size"+size);
                        html5 = '<span class="item">' + "Folder Size: " + '<b>' + size + '</b>' + '</span>';
                        return html5;


               }
          });
}



})();

but after searching on internet,I learned that jsonGet is asynchronous,so it is exceuted after the synchronous code.so,I replaced the jsonGet method with $.ajax method and set the async to false to make $.ajax synchronous and access the variable outside the jsonGet method.but,still the variables "size" is undefined.

any help would be greatly appreciated.I am trying dis from many days

  • You definitely *don't* want to use `async: false`. (I wouldn't be surprised if any given browser simply stops supporting it.) Whatever operations you want to perform in response to the asynchronous operation should be performed by or invoked by your callback. – David Sep 07 '17 at 12:37
  • $.ajax is asynchronous too, in javascript most functions are. Solutions : - Use a Promise either with bluebird or ES6 - use ES8 and make the function `await` . I don't know if the `async:false` option even still works – DarkMukke Sep 07 '17 at 12:40

0 Answers0