You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by smallufo <sm...@gmail.com> on 2008/05/10 23:50:23 UTC

Redundant method only works for PropertyModel

I have a DataInputPanel extends FormComponentPanel which shows a
CityDropDownChoice extends DropDownChoice :


public class DataInputPanel extends FormComponentPanel
{
  private CityDropDownChoice cityDropDownChoice;

  public DataInputPanel(String id , IModel model)
  {
    super(id , model);

    cityDropDownChoice = new CityDropDownChoice("city" , new
PropertyModel(model , "city") , Arrays.asList(Cities.values()));
    add(cityDropDownChoice);
  }
}

I found this doesn't work , because no matter the user selects , it always
shows the default city in the model.
That is , model.setCity(...) is always not called.

So I change the code to :

public class DataInputPanel extends FormComponentPanel
{
  private CityDropDownChoice cityDropDownChoice;

  public DataInputPanel(String id , IModel model)
  {
    super(id , model);

    cityDropDownChoice = new CityDropDownChoice("city" , new
PropertyModel(this , "myModel.city") , Arrays.asList(Cities.values()));
    add(houseSystemDropDownChoice);
  }

  public IModel getMyModel()
  {
    return getModel();
  }
}

It works , but it is ugly !
Because the getMyModel() method is in fact , redundant.
It just works for PropertyModel(this , "myModel.city")

Is it a design fault ?
Is there any way to get rid of this method ?

Re: Redundant method only works for PropertyModel

Posted by smallufo <sm...@gmail.com>.
2008/5/11 Johan Compagner <jc...@gmail.com>:

>
> But if a PropertyModel sees a IModel as its object
> then it will call getObject() and on that it will evaluate "city"
> So what is getObject() returning MyModel??
>
> You shouldnt mix these things.
>
> if you want the city object from a model it should go like this:
>
> mymodel.getObject().getCity() not mymodel.getCity()
>
> johan
>

It solves my confusion...
Thanks a lot!

Re: Redundant method only works for PropertyModel

Posted by Johan Compagner <jc...@gmail.com>.
>
> Well , it is in fact a my custom MyModel extends Model ,
> besides getCity() , setCity(...) , there are still other getter/setters out
> there.
> That's why I wrote PropertyModel(model , "city") there.
>

But if a PropertyModel sees a IModel as its object
then it will call getObject() and on that it will evaluate "city"
So what is getObject() returning MyModel??

You shouldnt mix these things.

if you want the city object from a model it should go like this:

mymodel.getObject().getCity() not mymodel.getCity()

johan

Re: Redundant method only works for PropertyModel

Posted by smallufo <sm...@gmail.com>.
2008/5/11 Maurice Marrink <ma...@gmail.com>:

> On Sun, May 11, 2008 at 12:54 AM, smallufo <sm...@gmail.com> wrote:
> > 2008/5/11 Maurice Marrink <ma...@gmail.com>:
> >
> >
> >  > PropertyModel is aware of the IModel you are passing and unwraps it.
> >  > So depending on what is inside your model you have different options:
> >  > -the model contains an object x which has a city property:
> >  >  make sure x has a getter and setter for city this will be used by the
> >  > propertymodel
> >  > -the model contains a city object:
> >  >  do not use a propertymodel at all or use "" as the expression.
> >  >
> >
> >  This (second) is my situation.
> >  This is my custom model object extends Model , which has getCity() ,
> >  setCity(...) methods.
> >  If I don't use PropertyModel , what should I do ?
>
> Ok so your model contains a city object. You can delete the
> get/setCity methods on the model wicket code will not use them.
> The get/setObject methods are used instead. You could even use the
> default Model if your custom model does not do anything else.
> And then do: new CityDropDownChoice("city" , model,
> Arrays.asList(Cities.values()));
> There is no need for a propertymodel there, since you are interested
> in replacing the entire object.
>
>
Well , it is in fact a my custom MyModel extends Model ,
besides getCity() , setCity(...) , there are still other getter/setters out
there.
That's why I wrote PropertyModel(model , "city") there.

Re: Redundant method only works for PropertyModel

Posted by Maurice Marrink <ma...@gmail.com>.
On Sun, May 11, 2008 at 12:54 AM, smallufo <sm...@gmail.com> wrote:
> 2008/5/11 Maurice Marrink <ma...@gmail.com>:
>
>
>  > PropertyModel is aware of the IModel you are passing and unwraps it.
>  > So depending on what is inside your model you have different options:
>  > -the model contains an object x which has a city property:
>  >  make sure x has a getter and setter for city this will be used by the
>  > propertymodel
>  > -the model contains a city object:
>  >  do not use a propertymodel at all or use "" as the expression.
>  >
>
>  This (second) is my situation.
>  This is my custom model object extends Model , which has getCity() ,
>  setCity(...) methods.
>  If I don't use PropertyModel , what should I do ?

Ok so your model contains a city object. You can delete the
get/setCity methods on the model wicket code will not use them.
The get/setObject methods are used instead. You could even use the
default Model if your custom model does not do anything else.
And then do: new CityDropDownChoice("city" , model,
Arrays.asList(Cities.values()));
There is no need for a propertymodel there, since you are interested
in replacing the entire object.


>
>  And ... how to use "" as the expression ?
>  I tries this :
>  MyModel mymodel = (MyModel)model;
>
> CityDropDownChoice = new CityDropDownChoice("city" , new
>  PropertyModel(myModel.getCity() , ""));
>
>  It still doesn't work , the selected value is not set to myModel.
>

Just forget that, it needlessly makes things complex in your situation.



Maurice

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


Re: Redundant method only works for PropertyModel

Posted by smallufo <sm...@gmail.com>.
2008/5/11 Maurice Marrink <ma...@gmail.com>:

> PropertyModel is aware of the IModel you are passing and unwraps it.
> So depending on what is inside your model you have different options:
> -the model contains an object x which has a city property:
>  make sure x has a getter and setter for city this will be used by the
> propertymodel
> -the model contains a city object:
>  do not use a propertymodel at all or use "" as the expression.
>

This (second) is my situation.
This is my custom model object extends Model , which has getCity() ,
setCity(...) methods.
If I don't use PropertyModel , what should I do ?

And ... how to use "" as the expression ?
I tries this :
MyModel mymodel = (MyModel)model;
CityDropDownChoice = new CityDropDownChoice("city" , new
PropertyModel(myModel.getCity() , ""));

It still doesn't work , the selected value is not set to myModel.

Re: Redundant method only works for PropertyModel

Posted by Maurice Marrink <ma...@gmail.com>.
PropertyModel is aware of the IModel you are passing and unwraps it.
So depending on what is inside your model you have different options:
-the model contains an object x which has a city property:
 make sure x has a getter and setter for city this will be used by the
propertymodel
-the model contains a city object:
 do not use a propertymodel at all or use "" as the expression.

Maurice

On Sat, May 10, 2008 at 11:50 PM, smallufo <sm...@gmail.com> wrote:
> I have a DataInputPanel extends FormComponentPanel which shows a
> CityDropDownChoice extends DropDownChoice :
>
>
> public class DataInputPanel extends FormComponentPanel
> {
>  private CityDropDownChoice cityDropDownChoice;
>
>  public DataInputPanel(String id , IModel model)
>  {
>    super(id , model);
>
>    cityDropDownChoice = new CityDropDownChoice("city" , new
> PropertyModel(model , "city") , Arrays.asList(Cities.values()));
>    add(cityDropDownChoice);
>  }
> }
>
> I found this doesn't work , because no matter the user selects , it always
> shows the default city in the model.
> That is , model.setCity(...) is always not called.
>
> So I change the code to :
>
> public class DataInputPanel extends FormComponentPanel
> {
>  private CityDropDownChoice cityDropDownChoice;
>
>  public DataInputPanel(String id , IModel model)
>  {
>    super(id , model);
>
>    cityDropDownChoice = new CityDropDownChoice("city" , new
> PropertyModel(this , "myModel.city") , Arrays.asList(Cities.values()));
>    add(houseSystemDropDownChoice);
>  }
>
>  public IModel getMyModel()
>  {
>    return getModel();
>  }
> }
>
> It works , but it is ugly !
> Because the getMyModel() method is in fact , redundant.
> It just works for PropertyModel(this , "myModel.city")
>
> Is it a design fault ?
> Is there any way to get rid of this method ?
>

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