Nabil Malloul Nabil Malloul - 1 year ago 104
Javascript Question

get Alfresco.util.Ajax.request response.json data from external function

I have an alfresco webscript who return a json response.

I have a js function

getWorkflowRepositoryContent()
who call this webscript and get the data retuned in the response.

I store the
response.json
in an array
list
.

All works fine for me, but when i call
getWorkflowRepositoryContent()
from another js function, it returned an empty array when it must return an array containing the data received from webscript
response
.

There is the function where i return the data received from the webscript.

Can you tell me what i made a mistake, or tell me how to properly return the data from that function.

function getWorkflowRepositoryContent(){
var list=[];
var workflowFilesNameAndNodeRef;

var test=function getWorkflowFilesList(response)
{
workflowFilesNameAndNodeRef=response.json.nodes;
$.each(response.json.nodes,function(index,value){
list.push(value.name);
});

}

Alfresco.util.Ajax.request(
{
method:Alfresco.util.Ajax.GET,
url: Alfresco.constants.PROXY_URI + "/ALFRESCO-DIRECTORY",
successCallback:
{
fn:test,
scope:this
},
failureCallback:
{
fn: function(response)
{
Alfresco.util.PopupManager.displayMessage({text:"Failure"});
},
scope: this
}
});
console.log(list.length);
return list;
}

Answer Source

Your getWorkflowRepositoryContent is getting asynchronous data but returning synchronously so your example won't work.

An easy way would be to simple call your function with a callback argument.

function getWorkflowRepositoryContent(cb){ // pass a callback as an argument
    var list=[];
    var workflowFilesNameAndNodeRef;

    var test=function getWorkflowFilesList(response)
    {
        workflowFilesNameAndNodeRef=response.json.nodes;
       console.log(response.json.nodes);
       $.each(response.json.nodes,function(index,value){
           list.push(value.name); 
       });


        $.each(list,function(index, fileName){
            $('<option/>').val(fileName).html(fileName).appendTo('#saveButton');
            $('<option/>').val(fileName).html(fileName).appendTo('#loadButton');
         });
         cb(list); // call the callback once the work is done
    }

    Alfresco.util.Ajax.request(
     {
        method:Alfresco.util.Ajax.GET, 
        url: Alfresco.constants.PROXY_URI + "/ALFRESCO-DIRECTORY",
        successCallback:
        {
           fn:test,
             scope:this
        },
        failureCallback:
        {
           fn: function(response)
           {
               Alfresco.util.PopupManager.displayMessage({text:"Failure To get StarXpert Workflow content"});
            },
           scope: this
        }
     });
}  

getWorkflowRepositoryContent( function(list) {

    console.log(list);
});

You could also use promises but it might be a little harder if you're not familiar with them.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download