I'm trying to retrieve the content of a javascript variable (array) in the JS widget code of a Primefaces custom component... I know I'm missing steps in the component and its renderer java code!
As a matter of fact, I want to add a completion feature (among others) on the Primefaces Terminal based on a list of commands stored in a javascript array and which I'd like to be provided in an attribute of the extended terminal component eg: <my:terminal completion="commandsList" />
having: var commandsList = ["foo", "bar"];
Here's the code so far:
@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!
},
Any help will be really appreciated, I know I could access commands list array directly from widget's init function, but I'd really like to ensure extensibility. Many thanks in advance!
As what I consider a workaround, I manage to do it using passthrough arguments, this way:
<my:terminal ... pt:completion="commandsList" />
with xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
and retrieving it in the widget's javascript code by evaluating 'completion' attribute value which is the name of the javascript array:
PrimeFaces.widget.MyTerminal = PrimeFaces.widget.Terminal.extend({
init : function(cfg) {
this._super(cfg);
commandsList = window[this.jq.attr('completion')];
},
... but I'm not satisfied with this solution, seems like patch up job to me :s
Edit : changed to window[varname]
instead of eval(varname)
... at least safer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.