10

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.

Community
  • 1
  • 1
Anatoly
  • 5,056
  • 9
  • 62
  • 136
  • What's your PrimeFaces version ? and what function are you trying to override ? – Hatem Alimam Dec 19 '14 at 10:15
  • Hi, I'm using PrimeFaces 5.1, I'm trying to override bindEditEvents function in DataTable https://github.com/primefaces/primefaces/blob/master/src/main/resources/META-INF/resources/primefaces/datatable/datatable.js#L1547 – Anatoly Dec 19 '14 at 10:34

2 Answers2

16

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

PrimeFaces.widget.DataTable.prototype.bindEditEvents = function() {
  ....
}
Hatem Alimam
  • 9,968
  • 4
  • 44
  • 56
7

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 entire class with its methods)

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').load = function() {

    // do whatever you prefer to extend it here

    // call the generic implementation
    PrimeFaces.widget.DataScroller.prototype.load.call(this, arguments);
};

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
            }
    };

Solution #4 (overriding init method of component)

if(PrimeFaces.widget.DataScroller) {
    PrimeFaces.widget.DataScroller.prototype.init = function(cfg) {
        PrimeFaces.widget.DeferredWidget.prototype.init.call(this, cfg);

        this.cfg = cfg;

        // this._super(cfg);

        // original init method code without calling _super method

        // apply here your custom code

    }
}
Oleksa O.
  • 827
  • 8
  • 16