【问题标题】:How do I find and/or override JavaScript in Primefaces component based on widgetVar?如何在基于 widgetVar 的 Primefaces 组件中查找和/或覆盖 JavaScript?
【发布时间】:2015-02-17 12:43:42
【问题描述】:

根据这个问题:Multiple file upload with extra inputText我可以通过这种方式使用widgetVar覆盖PrimeFaces元素的JavaScript函数:

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

现在,我尝试覆盖 DataTable 中的函数,但不明白如何引用它?此外,PF(') 从 chrome 调试器控制台返回 undefined,所以我无法调试它。我怀疑这是范围的问题,但不知道如何解决。

【问题讨论】:

标签: javascript jsf-2 primefaces


【解决方案1】:

还有更多解决方案。看你想走多深。

就我而言,我打算扩展 DataScroller 的功能。

尽管回答您的问题为时已晚,但我希望以下解决方案对其他人有所帮助:

解决方案 #1 (扩展当前方法并添加您自己的方法)

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

解决方案 #2 (将现有方法扩展到特定小部件)

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

    // do whatever you prefer to extend it here

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

解决方案 #3 (通过原型扩展现有方法)

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

【讨论】:

    【解决方案2】:

    你可以使用原型, 例如覆盖bindEditEvents 看起来像这样

    PrimeFaces.widget.DataTable.prototype.bindEditEvents = function() {
      ....
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-13
      • 1970-01-01
      • 2014-08-19
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多