You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Boris Goldowsky <bg...@cast.org> on 2016/12/05 15:34:22 UTC

Annotation for detachable field

Is there any way to create an annotation that would mark a field’s value as something that ought to be detached?  That is, instead of:

private IModel<User> userModel;

                @Override
                public void onDetach() {
                                super.onDetach();
                                if (userModel != null)
                                                userModel.detach();
                }

I would like to be able to write simply:

                @Detach
private IModel<User> userModel;

Has anyone tried this?

Boris


Re: Annotation for detachable field

Posted by Peter Henderson <pe...@starjar.com>.
On 6 December 2016 at 10:05, Martin Grigorov <mg...@apache.org> wrote:

> http://markmail.org/message/qttwc5kunubl6ieo



Oh I'm glad auto detach was not included way back when I started learning
Wicket.
Coming from a legacy swing app which spoon fed components what to display
and how/when to display it,
I found wickets model approach really odd. I took, what seems, ages to
fully grok the idea. Property Models really muddied my water, to such an
extent I never use them.

I've many many panels which can either being displaying data or editing it.
When editing onDetach does not detach the underlying LoadableDetachableModel
When displaying detaching works as expected.

This way I can load and detach an entity from JPA and store it in a LDM.


Peter.

Re: Annotation for detachable field

Posted by Martin Grigorov <mg...@apache.org>.
http://markmail.org/message/qttwc5kunubl6ieo

Martin Grigorov
Wicket Training and Consulting
https://twitter.com/mtgrigorov

On Mon, Dec 5, 2016 at 4:46 PM, Martijn Dashorst <martijn.dashorst@gmail.com
> wrote:

> This would not be any more efficient than scanning for fields that
> implement IDetachable. Which is: rather inefficient for when you have
> component hierarchies of 100-1000s of components.
>
> We have created a utility method at $$$ job that removes the need for
> the null check, and takes into account arrays and lists:
>
> protected void onDetach() {
>     detachQuietly(model1);
>     detachQuietly(model2);
> }
>
> public static void detachQuietly(Object detachable)
> {
>     if (detachable instanceof Component)
>     {
>         ((Component) detachable).detach();
>     }
>     else if (detachable instanceof IDetachable)
>     {
>         ((IDetachable) detachable).detach();
>     }
>     else if (detachable instanceof Map)
>     {
>         for (Map.Entry< ? , ? > entry : ((Map< ? , ? >)
> detachable).entrySet())
>         {
>             detachQuietly(entry.getKey());
>             detachQuietly(entry.getValue());
>         }
>     }
>     else if (detachable instanceof Iterable)
>     {
>         Iterator< ? > iter = ((Iterable< ? >) detachable).iterator();
>         while (iter.hasNext())
>         {
>             detachQuietly(iter.next());
>         }
>     }
>     else if (detachable instanceof Object[])
>     {
>         Object[] array = (Object[]) detachable;
>         for (Object curObj : array)
>         {
>             detachQuietly(curObj);
>         }
>     }
> }
>
>
>
> On Mon, Dec 5, 2016 at 4:34 PM, Boris Goldowsky <bg...@cast.org>
> wrote:
> > Is there any way to create an annotation that would mark a field’s value
> as something that ought to be detached?  That is, instead of:
> >
> > private IModel<User> userModel;
> >
> >                 @Override
> >                 public void onDetach() {
> >                                 super.onDetach();
> >                                 if (userModel != null)
> >                                                 userModel.detach();
> >                 }
> >
> > I would like to be able to write simply:
> >
> >                 @Detach
> > private IModel<User> userModel;
> >
> > Has anyone tried this?
> >
> > Boris
> >
>
>
>
> --
> Become a Wicket expert, learn from the best: http://wicketinaction.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Annotation for detachable field

Posted by Martijn Dashorst <ma...@gmail.com>.
This would not be any more efficient than scanning for fields that
implement IDetachable. Which is: rather inefficient for when you have
component hierarchies of 100-1000s of components.

We have created a utility method at $$$ job that removes the need for
the null check, and takes into account arrays and lists:

protected void onDetach() {
    detachQuietly(model1);
    detachQuietly(model2);
}

public static void detachQuietly(Object detachable)
{
    if (detachable instanceof Component)
    {
        ((Component) detachable).detach();
    }
    else if (detachable instanceof IDetachable)
    {
        ((IDetachable) detachable).detach();
    }
    else if (detachable instanceof Map)
    {
        for (Map.Entry< ? , ? > entry : ((Map< ? , ? >) detachable).entrySet())
        {
            detachQuietly(entry.getKey());
            detachQuietly(entry.getValue());
        }
    }
    else if (detachable instanceof Iterable)
    {
        Iterator< ? > iter = ((Iterable< ? >) detachable).iterator();
        while (iter.hasNext())
        {
            detachQuietly(iter.next());
        }
    }
    else if (detachable instanceof Object[])
    {
        Object[] array = (Object[]) detachable;
        for (Object curObj : array)
        {
            detachQuietly(curObj);
        }
    }
}



On Mon, Dec 5, 2016 at 4:34 PM, Boris Goldowsky <bg...@cast.org> wrote:
> Is there any way to create an annotation that would mark a field’s value as something that ought to be detached?  That is, instead of:
>
> private IModel<User> userModel;
>
>                 @Override
>                 public void onDetach() {
>                                 super.onDetach();
>                                 if (userModel != null)
>                                                 userModel.detach();
>                 }
>
> I would like to be able to write simply:
>
>                 @Detach
> private IModel<User> userModel;
>
> Has anyone tried this?
>
> Boris
>



-- 
Become a Wicket expert, learn from the best: http://wicketinaction.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org