简体   繁体   中英

Access variable from inner onClick method in outer class in Wicket

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

}

  1. The idea is to use a shareable container for the content . In Wicket this is the model ( IModel ).
  2. When the link is clicked then you update the model
  3. To update the Label you need to add it to the AjaxRequestTarget.
  4. Don't forget to make the Label Ajax updateable by calling .setOutputMarkupId(true) on it
  5. By using target.appendJavaScript(CharSequence) you can send the newly calculated content to the browser

Simple 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM