> it's more like an model graph.. so you say
>
> IModel<List<Something>> dataFromDB=new LoadableDetachedModel() ...
> IModel<Integer> countModel=new
> CascadingLoad..Model<Integer,List<Something>>(dataFromDB);
> add(new Label("counter",countModel));
>
> countModel.detach() is called from Label, and dataFromDB.detach() is
> called fram countModel.
>
> Because it's generic you can use it everywhere..
>

Ok. I came up with something similar based on the assumption: models
do not change during render.

I made a ModelLatch that caches the model value after onBeforeRender
and frees the latch at onDetach.

The problem is that there is no "onBeforeRender" event and one must
implement it for each container at least and for components that need
it and are ajax updated without their parent container:


public class ModelLatch implements IDetachable {
  private boolean latched;
  private boolean strict = true;

  /**
   *
   */
  public void onBeforeRender() {
    this.setLatched(true);
  }

  /**
   * @see org.apache.wicket.model.IDetachable#detach()
   */
  public void detach() {
    this.setLatched(false);
  }

  /**
   * @param <DataType>
   * @param <DataTypeModel>
   * @param dataTypeModel
   * @return IModel<DataType>
   */
  public <DataType, DataTypeModel extends IModel<DataType>>
IModel<DataType> getInstance(
      DataTypeModel dataTypeModel) {
    return new LatchModel<DataType>(dataTypeModel);
  }

  /**
   * @param latched the latched to set
   */
  public void setLatched(boolean latched) {
    this.latched = latched;
  }

  /**
   * @return the latched
   */
  public boolean isLatched() {
    return latched;
  }

  /**
   * @author Martin
   *
   * @param <T>
   */
  private class LatchModel<T> implements IModel<T> {
    private IModel<T> rootModel;
    private boolean cached;
    private T cache;

    /**
     * @param rootModel
     */
    public LatchModel(IModel<T> rootModel) {
      this.rootModel = rootModel;
    }

    /**
     * @see org.apache.wicket.model.IModel#getObject()
     */
    @Override
    public T getObject() {
      if (isLatched()) {
        return getCachedValue();
      }

      return rootModel.getObject();
    }

    /**
     * @return T
     */
    private T getCachedValue() {
      if (cached) {
        return cache;
      }

      try {
        return cache = rootModel.getObject();
      } finally {
        cached = true;
      }
    }

    /**
     * @see org.apache.wicket.model.IModel#setObject(java.lang.Object)
     */
    @Override
    public void setObject(T object) {
      if (isLatched() && isStrict()) {
        throw new IllegalStateException("Strict latch does not allow
modifying values in latched state.");
      }
      rootModel.setObject(object);
    }

    /**
     * @see org.apache.wicket.model.IDetachable#detach()
     */
    @Override
    public void detach() {
      setLatched(false);
      cached = false;
    }
  }

  /**
   * @return the strict
   */
  public boolean isStrict() {
    return strict;
  }
}

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

Reply via email to