Javascript MonkeyPatching:PrimeFaces小部件扩展/覆盖
我目前正在使用(工作正常) 但我想使用一些更具可读性的“jQuery-ish”代码,比如完全虚构的不切实际的代码:Javascript MonkeyPatching:PrimeFaces小部件扩展/覆盖,javascript,jquery,jsf,primefaces,monkeypatching,Javascript,Jquery,Jsf,Primefaces,Monkeypatching,我目前正在使用(工作正常) 但我想使用一些更具可读性的“jQuery-ish”代码,比如完全虚构的不切实际的代码: PrimeFaces.widget.OverlayPanel.patch( { init: function(cfg) { super.init(cfg); this.align(); }, show: function() { console.log('blah blah blah');
PrimeFaces.widget.OverlayPanel.patch(
{
init: function(cfg)
{
super.init(cfg);
this.align();
},
show: function()
{
console.log('blah blah blah');
super.show();
}
});
我尝试了PrimeFaces.widget.Xxx.extend({…})
,但在这种情况下,我无法访问super
方法
请记住,我完全不懂Javascript
谢谢我已经做到了:
if(PrimeFaces.widget.OverlayPanel)
{
PrimeFaces.widget.OverlayPanel = PrimeFaces.widget.OverlayPanel.extend(
{
init: function(cfg)
{
this._super(cfg);
this.align();
},
show: function()
{
console.log('blah blah blah');
this._super();
}
});
};
这是储存在里面的
- 应用程序
- 资源
- js
- pf-patches.js
- js
- 资源
<!DOCTYPE html>
<html lang="#{userBean.locale.language}" xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:cc="http://xmlns.jcp.org/jsf/composite"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core" xmlns:fn="http://xmlns.jcp.org/jsp/jstl/functions"
xmlns:p="http://primefaces.org/ui" xmlns:pe="http://primefaces.org/ui/extensions"
xmlns:o="http://omnifaces.org/ui" xmlns:of="http://omnifaces.org/functions"
xmlns:s="http://shapeitalia.com/jsf2" xmlns:sc="http://xmlns.jcp.org/jsf/composite/shape"
xmlns:e="http://java.sun.com/jsf/composite/cc" xmlns:et="http://shapeitalia.com/edea2"
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough">
<f:view locale="#{userBean.locale}" contentType="text/html">
<h:head>
<f:facet name="first">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<title><ui:insert name="title">#{navigatorBean.viewTitle}</ui:insert></title>
<link rel="shortcut icon" type="image/x-icon" href="#{resource['favicon/favicon.ico']}" />
</f:facet>
<f:facet name="last">
==========> <h:outputScript name="js/pf-patches.js" /> <==========
<h:outputScript library="omnifaces" name="fixviewstate.js" />
</f:facet>
</h:head>
<h:body>
<h:outputStylesheet name="theme/custom.css" />
<h:outputStylesheet name="theme/other-icons.css" />
...
#{navigatorBean.viewTitle}
=======>我也遇到了同样的问题,但您的解决方案对我的问题没有100%的效率(请参见此处),因为您愿意从OverlyPanel
调用\u super()
,而我正试图避免它(它做了一些我不想做的事情)。所以我所做的是:
/*快速修复,以便在对话组件PF#1928中处理布局*/
if(PrimeFaces.widget.Layout){
PrimeFaces.widget.Layout.prototype.init=函数(cfg){
//故意跳过调用PrimeFaces.widget.Layout.init()
PrimeFaces.widget.DeferredWidget.prototype.init.call(this,cfg);
this.cfg=cfg;
this.id=this.cfg.id;
this.jqId=PrimeFaces.escapeClientId(this.id);
如果(this.cfg.full){//full
var dialog=$(this.cfg.center.paneSelector).parents('.ui dialog content');
如果(对话框){//full+对话框
this.jq=对话框;
}否则{
this.jq=$('body');
}
}如果(this.cfg.parent){//nested
this.jq=$(PrimeFaces.escapeClientId(this.cfg.parent));
}else{//元素
this.jq=$(this.jqId);
}
this.renderFerred();
}
}
这样,init()
就会被完全覆盖。我希望它能在这种非典型的情况下帮助很少人;) 你说“所有这类小部件”是什么意思?所有重叠面板或所有其他类型的面板是否都要?如果是前者,只需将其包含在页面的模板中即可。你是对的,这是一个无稽之谈-从问题中删除,在小部件声明之后和PrimeFaces.cw(…)实例化之前包含脚本就足够了。好的,清除。我也试过一次“扩展”,但也没有成功。通常,更漂亮的代码更好,但在这种情况下,我不会花时间进一步研究,而是按照它的工作方式使用它(它没有那么难看;-)。你把它放在哪里?(顺便说一句,(1.5年前)如果我没记错的话,这个超级(cfg)对我不起作用)。。。我注意到您只在超级调用之后添加功能。。。也许这在当时是可行的,但我需要更改其中的代码。。。很好,但它可以工作,感谢您的反馈,正如您在问题中看到的,目标实际上是访问super
方法。但是,在我自己的答案中使用代码而不调用this.\u super(…)
,会导致完全重写。出于这两个原因,我不知道你的答案有什么附加值。嗯,我一直在寻找解决我问题的方法。通过本文,我阅读了“扩展/覆盖JS中的PrimeFaces.widget”,这正是我想要做的。你的解决方案帮助我开始,我只是觉得你的问题和我的不完全一样。但还是值得分享解决方案。对于这一点,它是一个附加值,对于想要的人来说,正如主题所说的“扩展/覆盖PrimeFaces小部件”。如果你仍然认为,这是无用的,我会简单地删除我的帖子(因为我不是想直接回答你的问题,而是给它添加一个价值,它可能会涉及到谁)。我仍然认为这正是我试图避免的,但它仍然是“合法”代码,我不是那个应该告诉你删除或离开它的人,你可以随心所欲。然而,我不会(也不能)删除我的否决票,除非你用一些有价值的内容编辑你的答案。很抱歉
<!DOCTYPE html>
<html lang="#{userBean.locale.language}" xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:cc="http://xmlns.jcp.org/jsf/composite"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core" xmlns:fn="http://xmlns.jcp.org/jsp/jstl/functions"
xmlns:p="http://primefaces.org/ui" xmlns:pe="http://primefaces.org/ui/extensions"
xmlns:o="http://omnifaces.org/ui" xmlns:of="http://omnifaces.org/functions"
xmlns:s="http://shapeitalia.com/jsf2" xmlns:sc="http://xmlns.jcp.org/jsf/composite/shape"
xmlns:e="http://java.sun.com/jsf/composite/cc" xmlns:et="http://shapeitalia.com/edea2"
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough">
<f:view locale="#{userBean.locale}" contentType="text/html">
<h:head>
<f:facet name="first">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<title><ui:insert name="title">#{navigatorBean.viewTitle}</ui:insert></title>
<link rel="shortcut icon" type="image/x-icon" href="#{resource['favicon/favicon.ico']}" />
</f:facet>
<f:facet name="last">
==========> <h:outputScript name="js/pf-patches.js" /> <==========
<h:outputScript library="omnifaces" name="fixviewstate.js" />
</f:facet>
</h:head>
<h:body>
<h:outputStylesheet name="theme/custom.css" />
<h:outputStylesheet name="theme/other-icons.css" />
...