cancel
Showing results for 
Search instead for 
Did you mean: 

How can i return a value from AJAX in Alfresco

skumar_us
Champ in-the-making
Champ in-the-making
Is it possible to return value from AJAX in alfresco.
I know it is possible outside alfresco by using callback ,which does not work here.is there any possible workaround ?I am able to get the value inside ajax call in the line "alert("getting value==>"docNodeRef);".But getting the value as "undefined" outside if it.which is expected since it is an asynchronous call.I am looking for some workaround to return the value from AJAX or can i make it synchronous,how?

loadNodeRef: function Tasklist_loadNodeRef(taskId)
      {
     
      Alfresco.util.Ajax.request(
      {
         url: Alfresco.constants.PROXY_URI + "/getNodeRef?taskId="+taskId,
        
         successCallback:
         {

           fn:function(response){
            var docNodeRef= response.serverResponse.responseText;
            
alert("getting value==>"docNodeRef);

   

            },
           scope: this
         },
         failureMessage: this.msg("message.failure"),
         scope: this,
         execScripts: true
      });   
},
5 REPLIES 5

scouil
Star Contributor
Star Contributor
Hi,

There are several ways to pass the value outside.
You passed your object in the scope, so you cals attached it to some custom property.
You could also use a closure to declare a variable before the function and have it in the closure to assign it's value and still have it available in the original variable.

But I think the real question is: What exactly are you trying to do and why can't it be done in the callback?

skumar_us
Champ in-the-making
Champ in-the-making
I have written a webscript to get the noderef by passing the taskid.I am trying to build the url with that noderef value which I will be sending to the workflow-edit page which is required to show the preview in the edit page.

below is my code with call back which is not working .I might be doing something wrong here.the line with "alert("outside==>"+originalNodeRef);" returns "undefined".this I am calling from a different function.
could you tell me how can i pass the object in the scope or how to use the closure? can you provide some example ??
 
var originalNodeRef = this.loadNodeRef(taskId,this.getNodeRef());
alert("outside==>"+originalNodeRef);   

loadNodeRef: function Tasklist_loadNodeRef(taskId,callback)
      {
     
      Alfresco.util.Ajax.request(
      {
         url: Alfresco.constants.PROXY_URI + "/getNodeRef?taskId="+taskId,
        
         successCallback:
         {

           fn:function(response){
            var docNodeRef= response.serverResponse.responseText;
            var strReplace = docNodeRef.replace(/"/gi, "");

            var node = "workspace" + strReplace .substring(strReplace .lastIndexOf(":"));
            var originalNodeRef =  node.replace("}","");
   alert(originalNodeRef);
   callback(originalNodeRef);

   

            },
           scope: this,
         },
         failureMessage: this.msg("message.failure"),
         scope: this,
         execScripts: true
      });   
},

getNodeRef:function Tasklist_getNodeRef(originalNodeRef) {
},

scouil
Star Contributor
Star Contributor
Hi,

Yes that is to be expected. your loadNodeRef function doesn't return anything.
Basically think of it this way:
If you did a synchronous call, it would have to wait for your whole call back and forth to the server is interpreted before finishing to run the script and display the rest of the form.
With Ajax you just finish to display the form and, whenever the call come back, you display your preview dynamically.

I guess that at the place where you did:
<code>alert("outside==>"+originalNodeRef);<code>
You expected to do something with the node that you retrieved.
Whatever this action is, could you do it instead here:
<code>alert(originalNodeRef);<code>

skumar_us
Champ in-the-making
Champ in-the-making
Hi,
I have to append the noderef value in the below href in the task-list.js(\Alfresco\tomcat\webapps\share\WEB-INF\classes\alfresco\site-webscripts\org\alfresco\components\workflow) inside renderCellTaskInfo function.  


href = $siteURL('task-edit?taskId=' + taskId + '&nodeRef='+originalNodeRef+ '&referrer=tasks&myTasksLinkBack=true') + '" class="theme-color-1" title="' + this.msg("link.editTask");


Since this reside inside renderCellTaskInfo function i really can't do this where i am doing the successcallback

it does work after declaring the originalNodeRef as global variable and having the alert  before building the href "alert(originalNodeRef);", But if I remove the alert it has the undefined value again.

sihnu
Champ in-the-making
Champ in-the-making
Hi,

try this:



successCallback : {
    fn : function (res) {
        var result = eval('(' + res.serverResponse.responseText + ')');
        alert("getting value==>" + result.docNodeRef);
    },
    scope : this
}


The successCallBack function is driven when the request gets it's response. I hope this helps.