Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the PF function do in Primefaces?

On many places one can find usage of a function PF with Primefaces. For example in this answer

From what I have seen so far it seems to be a magic "make it work a little better" function. But I don't believe in this kind of stuff so:

What does this function do?

And where can I find documentation about it?

like image 426
Jens Schauder Avatar asked May 15 '14 13:05

Jens Schauder


People also ask

What is widgetVar in PrimeFaces?

widgetVar is the name of the client side variables which contains all the javascript PF widgets on the page.

Does PrimeFaces use JQuery?

JQuery extensionsPrimeFaces relies heavily on JQuery for interacting with the DOM. Many JQuery plugins are included that are used by the various widgets.


3 Answers

PF is a Javascript function.

In Primefaces 4.0 the Javascript scope of widgets changed. Prior to version 4.0 you could open a dialog widget with widgetVar.show();.

In Primefaces 4.0 and above the widgets are stored in a Javascript widget array. When you call PF('widgetVar') it is looking for the widget in the array and returning it.

PF=function(d){
    var c=b.widgets[d];
    if(!c){
        if(a.console&&console.log){
            console.log("Widget for var '"+d+"' not available!")
        }
        b.error("Widget for var '"+d+"' not available!")
    }
    return c
};

I could not find much on this either this is what I was able to decipher using Chrome's developer tools.

like image 70
Mark Avatar answered Nov 10 '22 04:11

Mark


The PF function is a part of PrimeFaces's JavaScript API. It looks up a Javascript object that is the backbone of the JSF component on the client-side. Here is its definition (source):

PF = function(widgetVar) {      
        var widgetInstance = PrimeFaces.widgets[widgetVar];

        if (!widgetInstance) {
            PrimeFaces.error("Widget for var '" + widgetVar + "' not available!");
        }

        return widgetInstance;
    };

PF is a shortcut for PrimeFaces.widgets['someWidgetId'], which just looks-up a Javascript object in global scope, and so the Javascript object can also be retrieved using window['someWidgetId'].

The PrimeFaces's Javascript API has no official documentation online, so to understand what you can really "do" with the Javascript object, you'll need to take a deep dive into PrimeFaces.

See also

  • "Intro To PrimeFaces widgetVar" blog post
  • PrimeFaces source code
like image 24
DavidS Avatar answered Nov 10 '22 05:11

DavidS


For other Primefaces users coming here when upgrading to version 4.0 and above, it's possible to bypass the need to use PF('yourWidgetVar').someFunction() and just use yourWidgetVar.someFunction() directly as you would have before version 4.0. You just need the following configuration in web.xml:

<context-param>
    <param-name>primefaces.LEGACY_WIDGET_NAMESPACE</param-name>
    <param-value>true</param-value>
</context-param>

From the Primefaces User Guide:

Enables window scope so that widgets can be accessed using widgetVar.method() in addition to default PF namespace approach like PF('widgetVar').method().

Obviously you'd be susceptible to the namespace clash/pollution this feature was created to avoid, but it's useful if you want to migrate to a new version in little steps and isolate what incompatibilities the new version has introduced.

like image 40
Ryan Bennetts Avatar answered Nov 10 '22 04:11

Ryan Bennetts