I have a class which looks like this
private static class LinkTest extends WebMarkupContainer {
public LinkTest(String id) {
super(id);
AjaxLink<Object> link = new AjaxLink<>("link") {
@Override
public void onClick(AjaxRequestTarget target) {
String content = //... some function call
}
};
add(link);
// .. how to access "content" variable here
link.add(new Label("label", Model.of(content)));
add(new AttributeAppender("onclick",
new Model(format("alert('%s');", contentModel)), ";"));
}
}
I'd like to get access of "content" variable inside of LinkTest constructor. Any ideas how to do it?
private static class LinkTest extends WebMarkupContainer {
public LinkTest(String id) {
super(id);
// 1
IModel<String> contentModel = Model.of("initial value");
AjaxLink<String> link = new AjaxLink<>("link", contentModel) {
@Override
public void onClick(AjaxRequestTarget target) {
// 2
contentModel.setObject("a new value");
// 3
target.add(this.get("label"));
// 5
target.appendJavaScript("alert('"+contentModel.getObject()+"');");
}
};
add(link);
Label label = new Label("label", contentModel);
link.add(label);
//4
label.setOutputMarkupId(true);
}
}
content
. In Wicket this is the model ( IModel
)..setOutputMarkupId(true)
on ittarget.appendJavaScript(CharSequence)
you can send the newly calculated content
to the browserSimple to solve by sharing a model between both components.
private static class LinkTest extends WebMarkupContainer {
public LinkTest(String id) {
super(id);
IModel<String> contentModel = ()-> /* some function call */;
AjaxLink<String> link = new AjaxLink<>("link", contentModel) {
@Override
public void onClick(AjaxRequestTarget target) {
target.add(this);
target.appendJavaScript("alert('"+getModelObject()+"');");
}
};
add(link);
// .. how to access "content" variable here
link.add(new Label("label", contentModel));
}
}
an anonymous class can access to the members of its enclosing class. So you might do something like this
private static class LinkTest extends WebMarkupContainer {
//Here!
String content;
public LinkTest(String id) {
super(id);
AjaxLink<Object> link = new AjaxLink<>("link") {
@Override
public void onClick(AjaxRequestTarget target) {
content = //... some function call
}
};
add(link);
// .. how to access "content" variable here
link.add(new Label("label", Model.of(content)));
}
}
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.