Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Not A Problem
-
9.10.0
-
None
-
None
Description
In a page (extends webPage) when we replace a component in a panel with panel.replace or panel.addOrReplace, when it remove the old component, the method detach is called and will detach the parent model instead of it own model if present. it cause a reload of the model when the new composant is added.
So imagine you have a panel with 20 components using 1 detachablemodel (wit some db queries), and on submit or on action we want to add all components in panel (loaded by a class), all are using the same model loaded once on the first getModel call , then all components will use same model value, and then detach is called => ok
But if we have a button will submit a form, and we create new components and replace in our panel, on each replace method, the remove method will detach parent model, so then on each component it will load the model (so 20x load model called)
How to reproduce ?
take a helloworld projet, create a panel with one component Label using a detachable string model
in HomePage constructor set panel, then first add the component, then replace it then make something else (getObject for example), on replace method model in page will be detached and we have to reload it
public class HomePage extends WebPage { private static final long serialVersionUID = 1L; Panel panel ; IModel<String> model = new LoadableDetachableModel<String>() { @Override protected String load() { return "test"; } @Override protected void onDetach() { super.onDetach(); } }; public HomePage(final PageParameters parameters) { super(parameters); model.getObject(); add(new Label("version", getApplication().getFrameworkSettings().getVersion())); panel = new HomePanel( "homePanel"); //first time no problem panel.addOrReplace( new Label( "test", model ) ); add(panel); //use ame model loaded model.getObject(); //here will detach the model panel.addOrReplace( new Label( "test", model ) ); //load a new model model.getObject(); } }
and in panel
public class HomePanel extends Panel { Label label; public HomePanel(String id) { super( id ); } public HomePanel(String id, IModel<String> model) { super( id, model ); label = new Label( "test", model.getObject() ); add( label ); } }
in home page html set the panel
<span wicket:id="homePanel"> </span>
in panel html
<wicket:panel xmlns:wicket="http://wicket.apache.org"> User: <span wicket:id="test">dummy name</span><br/> </wicket:panel>
to go further on getModelImpl() (in component.java) we retrieve the first element in data object in the picture below we make the detach on the HomePanel Object replaced but we see the model detach it the page model
so why we detach the parent model instead of just let the page do this job ?