Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primefaces: Default oncomplete method for all ajax request

I am trying to configure an oncomplete method for all ajax requests so that I can handle session timeout.

I tried adding the following script but it didn't work the same way as setting oncomplete property for p:ajax element. It wouldn't execute each time an Ajax request is made.

$.ajaxSetup({method: post,
    complete: function(xhr, status, args){
        var xdoc = xhr.responseXML;
        if(xdoc == null){
            return;
        }
        errorNodes = xdoc.getElementsByTagName('error-name');

        if (errorNodes.length == 0) {
            return;
        }

        errorName = errorNodes[0].childNodes[0].nodeValue;
        errorValueNode = xmlDoc.getElementsByTagName('error-message');
        errorValue = errorValueNode[0].childNodes[0].nodeValue;

        alert(errorValue);
        document.location.href='${pageContext.request.contextPath}/login/login.jsf';
    }
});

Any help would be appreciated

like image 872
KunalC Avatar asked Dec 12 '22 18:12

KunalC


2 Answers

PrimeFaces newer versions (using PF 5 here)

var originalPrimeFacesAjaxUtilsSend = PrimeFaces.ajax.Request.send;
PrimeFaces.ajax.Request.send = function(cfg) {
    if (!cfg.oncomplete) {
        cfg.oncomplete = doYourStuff;
    }
    originalPrimeFacesAjaxUtilsSend.apply(this, arguments);
};

Just to keep it somewhere, tried to find at stackoverflow but only older versions.. hope someone find this useful.

like image 104
Simego Avatar answered Feb 20 '23 16:02

Simego


I managed to implement this by wrapping Primefaces AjaxUtils method.

var originalPrimeFacesAjaxUtilsSend = PrimeFaces.ajax.AjaxUtils.send;

   PrimeFaces.ajax.AjaxUtils.send = function(cfg) {
      if (!cfg.oncomplete) {
         // register default handler
         cfg.oncomplete = oncompleteDefaultHandler;
      }
      originalPrimeFacesAjaxUtilsSend.apply(this, arguments);
   };
like image 30
KunalC Avatar answered Feb 20 '23 15:02

KunalC