Problem with SearchPanel's Model

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with SearchPanel's Model

Daniel Toffetti
Hi Nathan,

    There is a problem with the way the SearchPanel's Model is used, here's the snippet (from memory):

    public SearchPanel(String id, IModel model) {
        ...
        this.setDefaultModel(model);
    }

    public MarkupContainer setDefaultModel(IModel model) {
        return search.setDefaultModel(model);
    }

    The problem here is that SearchPanel doesn't follow the contract for models inherited from Component. In other words, if I do:

    searchPanel.setDefaultModel(new Model<String>("mymodelstring"));

    and then:

    String modelObject = searchPanel.getDefaultModelObject();

    then modelObject woud be set to null. Since you delegate the SearchPanel's Model to the TextField, I guess the easiest solution would be to override also searchPanel.getDefaultModelObject() so that it returns search.getDefaultModelObject() (the model in the textfield).

    Hope this all makes sense, I can provide you with better info in a couple days if needed.

Cheers,

Daniel
Reply | Threaded
Open this post in threaded view
|

Re: Problem with SearchPanel's Model

n8han
Administrator
Hi Daniel, I wonder if you could fork the project on github and make the changes you suggest on a branch so I can pull them in?
http://github.com/n8han/Databinder-for-Wicket

Nathan
Reply | Threaded
Open this post in threaded view
|

Re: Problem with SearchPanel's Model

Daniel Toffetti
Hi Nathan,

    My previous description of the problem was not very accurate, but now I've managed to workaround the problem by calling the second SearchPanel constructor, anyway I guess the current code still has problems, see (untouched code):

public abstract class SearchPanel extends Panel {
       
        private TextField search;
       
        /**
         * @param id Wicket id
         */
        public SearchPanel(String id) {
                super(id, new Model());
                add(new SearchForm("searchForm"));
        }

        /** Use the given model (must not be read-only ) for the search string */
        public SearchPanel(String id, IModel searchModel) {
                super(id, searchModel);
                add(new SearchForm("searchForm"));
        }
       
        @Override
        /** Sets model to search component. */
        public MarkupContainer setDefaultModel(IModel model) {
                return search.setDefaultModel(model);
        }

    As you can see, calling the second constructor sets the SearchPanel's model by calling super constructor, but if you use the first constructor and then call setDefaultModel, then the SearchPanel's own model is never set to the same model as search TextField.
    One possible fix would be like this:

        @Override
        /** Sets model to search component. */
        public MarkupContainer setDefaultModel(IModel model) {
++ super.setDefautlModel(model);
                return search.setDefaultModel(model);
        }

    But then you would still need to call setDefaultModel even if you use the second constructor. A better solution would require some refactoring of both constructors and setDefaultModel method.
    Hope this helps,

Daniel