Hi,

In project wicket-contrib-data, there is TransientObjectDetachableModel. I find this model very usefull, and think that this model (maybe with a different name?) should be in wicket-core or wicket-extensions as in my opinion the current way of making a custom detachable models is just too verbose.

It handles the case where you want to load an object when onAttach is called, that object (transient) is returned on getObject, and onDetach, the object is set to null again. In my implementation, there is only one method to implement (load) opposed to the multiple methods when overriding AbstractDetachableModel.

An example of it's usage:

public final class HuidigeKlantModel extends TransientObjectDetachableModel
{
   protected Object load()
   {
       FmoDemoSession session = (FmoDemoSession)WebSession.get();
       return session.getHuidigeKlant();
   }
}

And this is the implementation:

import wicket.Component;
import wicket.model.AbstractDetachableModel;

/**
* Model that makes working with detachable models a breeze again.
* Holds a temporary, transient model object, that is set on 'onAttach' by calling
* abstract method 'load', and that will be reset/ set to null on 'onDetach'.
*
* A usage example:
* <pre>
* TransientObjectDetachableModel venueListModel = new TransientObjectDetachableModel()
* {
* protected Object load()
* {
* return getVenueDao().findVenues();
* } * };
* </pre>
*
* @author Eelco Hillenius
*/
public abstract class TransientObjectDetachableModel extends AbstractDetachableModel
{
/** temporary, transient object. */
private transient Object tempModelObject;


   /**
    * Construct.
    */
   public TransientObjectDetachableModel()
   {
       super();
   }

   /**
    * @see wicket.model.AbstractDetachableModel#onAttach()
    */
   protected final void onAttach()
   {
       this.setObject(load());
   }

   /**
    * Loads and returns the (temporary) model object.
    * @return the (temporary) model object
    */
   protected abstract Object load();

   /**
    * @see wicket.model.AbstractDetachableModel#onDetach()
    */
   protected final void onDetach()
   {
       tempModelObject = null;
   }

/**
* @see wicket.model.AbstractDetachableModel#onGetObject(wicket.Component)
*/
protected final Object onGetObject(Component component)
{
return tempModelObject;
}


   /**
    * Sets the object.
    * @param object the object
    */
   protected final void setObject(Object object)
   {
       setObject(null, object);
   }

/**
* @see wicket.model.AbstractDetachableModel#onSetObject(wicket.Component, java.lang.Object)
*/
protected final void onSetObject(Component component, Object object)
{
this.tempModelObject = object;
}


   /**
    * @see wicket.model.IModel#getNestedModel()
    */
   public final Object getNestedModel()
   {
       return tempModelObject;
   }
}

Votes please?

Eelco


------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click _______________________________________________ Wicket-develop mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/wicket-develop

Reply via email to