1

Is possible to get Primefaces version using Javascript or Jquery?

According to primeface 4 or newer, widgetVar objects are called through PF('widgetVar').something(). While primefaces 3.5 or lower did not support PF function. Thus, I want to write jquery funtion that support to call widgetVar of dialog through any primefaces version.

Does anyone have any ideas, please share. Thanks.

wittakarn
  • 3,124
  • 1
  • 18
  • 31
  • 1
    The version number can be obtained by [Java code](http://stackoverflow.com/q/18355985/1391249). You can make an AJAX request to fetch it from the server and use in your JavaScript/jQuery code. – Tiny Sep 30 '14 at 04:26
  • Thanks @Tiny to advise, I think your advise is only one solution to solve my problem. – wittakarn Sep 30 '14 at 04:30

2 Answers2

1

If you go to the PrimeFaces showcase and execute this in the console:

PrimeFaces.getFacesResource

You would get a hint on how PrimeFaces is getting the current version in Javascript, but unfortunately this function is only available in the showcase script.

So I had to rewrite a similar one.

function getPFVersion() {
   var src = $('script[src*="/javax.faces.resource/primefaces.js"]').attr("src"); 
   return new RegExp("v=([^&]*)").exec(src)[1];
}

You can also get the PrimeFaces version in the backend:

PrimeFaces 4.0 and higher

String pfVersion = RequestContext.getCurrentInstance().getApplicationContext().getConfig().getBuildVersion();

PrimeFaces 3.5 and lower

String pfVersion = org.primefaces.util.Constants.VERSION;
Hatem Alimam
  • 9,968
  • 4
  • 44
  • 56
  • Thanks to advise, the second solution, which is PrimeFaces version in the backend is work, better than my solution. Moreover, your javascript is work. – wittakarn Sep 30 '14 at 08:40
  • I found some problem. When I use RequestContext.getCurrentInstance().getApplicationContext().getConfig().getBuildVersion(); with primefaces 3.4.2, NoSuchMethodError exception occur. Thus, this backend proposed is not work with primefaces 3.4.2. – wittakarn Oct 09 '14 at 03:42
  • then you should catch the exception and try this [`org.primefaces.util.Constants.VERSION`](http://www.primefaces.org/docs/api/3.4/org/primefaces/util/Constants.html#VERSION) – Hatem Alimam Oct 09 '14 at 07:17
0

At last, I use resources bundle to store primefaces version which is built by maven.

This snippet code in Managedbean to check version and change pattern of jquery calling.

public String getPFPattern(final String widgetVar){
    String sPFVersion = getPrimefacesVersion();
    double iPFVersion = 0;
    String resp = widgetVar;
    if(sPFVersion != null && !sPFVersion.equals("")){
        iPFVersion = Double.parseDouble(sPFVersion);
    }
    if(iPFVersion >= 4){
        resp = "PF('".concat(resp).concat("')");
    }
    return resp;
}

public String getPrimefacesVersion(){
    ResourceBundle bundle = ResourceBundle.getBundle("com.xxx");
    return bundle.getString("primefacesVersion");
}

My XHTML

oncomplete="#{versionBean.getPFPattern('dlgReference')}.show()"

The propose method getPFPattern return PF('xxx') in case primefaces version 4 or newer

Hatem Alimam
  • 9,968
  • 4
  • 44
  • 56
wittakarn
  • 3,124
  • 1
  • 18
  • 31