我正在尝试在Primefaces自定义组件的JS小部件代码中检索javascript变量(数组)的内容...我知道我在组件及其渲染器java代码中缺少步骤!
事实上,我想根据存储在javascript数组中的命令列表在Primefaces Terminal上添加一个完成功能(以及其他功能),我希望在扩展的属性中提供该功能。终端组件
例如:<my:terminal completion="commandsList" />
拥有:var commandsList = ["foo", "bar"];
到目前为止,这是代码:
@ResourceDependencies({
@ResourceDependency(library="primefaces", name="terminal/terminal.css"),
@ResourceDependency(library="primefaces", name="primefaces.css"),
@ResourceDependency(library="primefaces", name="jquery/jquery.js"),
@ResourceDependency(library="primefaces", name="jquery/jquery-plugins.js"),
@ResourceDependency(library="primefaces", name="primefaces.js"),
@ResourceDependency(library="primefaces", name="terminal/terminal.js"),
@ResourceDependency(library="js", name="terminal.js")
})
@FacesComponent(MyTerminal.COMPONENT_TYPE)
public class MyTerminal extends Terminal {
public static final String COMPONENT_FAMILY = "com.jsf.components";
public static final String COMPONENT_TYPE = "com.jsf.components.MyTerminal";
@Override
public String getFamily() {
return COMPONENT_FAMILY;
}
@Override
public String getRendererType() {
return MyTerminalRenderer.RENDERER_TYPE;
}
private enum PropertyKeys {
completion
}
public String getCompletion() {
return (String) getStateHelper().eval(PropertyKeys.completion, null);
}
public void setCompletion(String completion) {
getStateHelper().put(PropertyKeys.completion, completion);
}
}
@FacesRenderer(
componentFamily = MyTerminal.COMPONENT_FAMILY,
rendererType = MyTerminalRenderer.RENDERER_TYPE
)
public class MyTerminalRenderer extends TerminalRenderer {
public static final String RENDERER_TYPE = "com.jsf.components.MyTerminalRenderer";
protected void encodeScript(FacesContext context, Terminal terminal) throws IOException {
String clientId = terminal.getClientId(context);
WidgetBuilder wb = getWidgetBuilder(context);
wb.init("MyTerminal", terminal.resolveWidgetVar(), clientId);
wb.finish();
}
@Override
public void encodeEnd(FacesContext context, UIComponent component)
throws java.io.IOException {
MyTerminal terminal = (MyTerminal) component;
/* TREATMENT HERE ? */
super.encodeEnd(context, component);
}
}
PrimeFaces.widget.MyTerminal = PrimeFaces.widget.Terminal.extend({
init : function(cfg) {
this._super(cfg);
// WOULD LIKE TO GET commandsList VAR HERE!
},
任何帮助都会非常感激,我知道我可以直接从widget的init函数访问命令列表数组,但我真的想确保可扩展性。 非常感谢提前!
答案 0 :(得分:0)
正如我认为的解决方法,我设法使用passthrough参数,这样:
<my:terminal ... pt:completion="commandsList" />
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
并通过评估'完成'属性值(即javascript数组的名称)在小部件的javascript代码中检索它:
PrimeFaces.widget.MyTerminal = PrimeFaces.widget.Terminal.extend({
init : function(cfg) {
this._super(cfg);
commandsList = window[this.jq.attr('completion')];
},
...但我对这个解决方案不满意,似乎对我来说是个补丁:s
修改:更改为window[varname]
而不是eval(varname)
...至少更安全。