Hi,

Here is yet another idea.

The following is a model that is a bit smarter than LoadableDetachableModel.
The extra bit is that it stores the model object in the RequestCycle's
metadata.
This way you may have N instances of this model used by N components in
your page but only the first one that asks for the model object will call
newsService.getNews().
All following components which need the model object will reuse it from the
cache (RequestCycle's metadata).
With a normal LDM you need to reuse the same instance of the model class
and this may lead to some coupling.

So, you may create an instance for the Page and another one for the Panel
that is updated via Ajax.

class NewsModel extends LoadableDetachableModel<List<NewsEntity>> {

    private static final MetaDataKey<List<NewsEntity>> NEWS_CACHE_KEY = new
MetaDataKey<List<NewsEntity>>() { };

    private final NewsService newsService;

    NewsModel(final NewsService newsService) {
        this.newsService = newsService;
    }

    @Override
    protected List<NewsEntity> load() {
        final RequestCycle requestCycle = RequestCycle.get();
        List<NewsEntity> news = null;
        if (requestCycle != null) {
            news = requestCycle.getMetaData(NEWS_CACHE_KEY);
        }

        if (news == null) {
            news = newsService.getNews(getUser());
            if (requestCycle != null) {
                requestCycle.setMetaData(NEWS_CACHE_KEY, news);
            }
        }
        return news;
    }
}


On Wed, Sep 26, 2018 at 10:06 AM yvus <yves.courvois...@enata.com> wrote:

> Hi,
>
> Thanks for your answer. From reading your answer and the one of KB, I have
> the feeling that the best place to be sure that the pointers get deleted is
> to follow your suggestion: creation in Page#onConfigure, deletion in
> Page#onDetach. BUT imagine I have a panel in my page in which I have an
> Ajax
> request. Therefore, if I am not mistaken, I would
> - either have to call the pointers construction again in the
> Panel#onConfigure, but that would lead to duplicates of the pointers (which
> I don't want),
> - either keep the pointers the same as in the Parent page, but then the
> parent page onDetach would kill them. And hence my Ajax request in the
> panel
> would have no access to the pointers, correct?
>
> Following your leads I would then propose to override a
> LoadableDetachableModel that would generate the pointers in the onAttach
> and
> delete them in the onDetach. This would imply many re-creation of the
> pointers though...
>
> In your second suggestion, you provide a way to keep the pointers alive
> during the Http Session, but this is the case for which we don't know when
> to delete the pointers, correct?
>
> Thanks again
>
>
>
> --
> Sent from:
> http://apache-wicket.1842946.n4.nabble.com/Users-forum-f1842947.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>

Reply via email to