سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top