Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find and/or override JavaScript in Primefaces component based on widgetVar?

According to this question: Multiple file upload with extra inputText I can override JavaScript function of PrimeFaces element by using widgetVar in this way:

PF('fileUpload').jq.fileupload({
    add: function(e, data) {
           ...
         }
    });

Now, I try to override function in DataTable and can't understand how do I reference to it? Moreover, PF(') returns undefined from chrome debugger console, so I can't debug it. I suspect that it's matter of scopes but don't know how to solve it.

like image 371
Anatoly Avatar asked Dec 18 '14 16:12

Anatoly


People also ask

What is prime faces for JSF?

PrimeFaces for JSF. PrimeFaces is a popular open source framework for JavaServer Faces featuring over 100 components, touch optimized mobilekit, client side validation, theme engine and more. PrimeNG for Angular. PrimeNG, a spin-off project from PrimeFaces, is a collection of open source rich UI components for Angular.

What is PrimeFaces?

1. Introduction Primefaces is an open source UI component suite for Java Server Faces (JSF) applications. In this tutorial, we'll give an introduction to Primefaces, and demonstrate how to configure it and use some of its main features. 2. Overview 2.1. Java Server Faces

How to override a JavaScript function?

How to override a JavaScript function ? Given an HTML document and the task is to override the function, either predefined function or user-defined function using JavaScript. Approach: When we run the script then Fun () function called.

What is the difference between prime faces and primeng?

PrimeFaces is a popular open source framework for JavaServer Faces featuring over 100 components, touch optimized mobilekit, client side validation, theme engine and more. PrimeNG for Angular. PrimeNG, a spin-off project from PrimeFaces, is a collection of open source rich UI components for Angular.


2 Answers

There are couple more solutions as well. Depends on how deep do you want to go.

In my case I was intending to extend DataScroller's functionality.

Despite it's too late answering your question, I hope the solutions below helps others:

Solution #1 (extending current methods and adding your own ones)

PrimeFaces.widget.DataScroller = PrimeFaces.widget.BaseWidget.extend({
    init: function(cfg) {
        this._super(cfg);
            
        // only for widget with widgetVar="yourWidgetVar"
        if(cfg.widgetVar === 'yourWidgetVar') {
            this.yourCustomMethod();
        }
    },

    yourCustomMethod: function() {
        // do whatever you prefer here
    }
});

Solution #2 (extending of already existing methods aimed to specific widgets)

PF('yourWidgetVar').init = function() {

    // do whatever you prefer to extend it here

    // call the generic implementation
    PrimeFaces.widget.DataScroller.prototype.init.call(this);
};

Solution #3 (extending of already existing methods via prototypes)

    const oldLoad = PrimeFaces.widget.DataScroller.prototype.load;

    PrimeFaces.widget.DataScroller.prototype.load = function() {
        oldLoad.apply(this, arguments);
    
        // do whatever you prefer to extend it here

        // in case you need to do it for specific widget. i.e. widgetVar="yourWidgetVar"
            if(cfg.widgetVar === 'yourWidgetVar') {
                // your custom stuff here
            }
    };
like image 52
Oleksa O. Avatar answered Oct 22 '22 20:10

Oleksa O.


You could use the prototype, for example overriding bindEditEvents would look like this

PrimeFaces.widget.DataTable.prototype.bindEditEvents = function() {
  ....
}
like image 41
Hatem Alimam Avatar answered Oct 22 '22 20:10

Hatem Alimam