Reputation: 1822
I want to create an ajaxcheckbox without wiring it to a class attribute using propertymodel. Why does the code in the example not work? The boolean value doesnt change when the user checks or unchecks the checkbox.
boolean show = false;
AjaxCheckBox showBox = new AjaxCheckBox("showBox", new Model<Boolean>(show)){
//onUpdate stuff
};
Upvotes: 2
Views: 2946
Reputation: 5150
The boolean value will not change because the showBox
does not have a reference to the original variable show
. You just initialized the showBox
model with false. The code you have there is equivalent to:
AjaxCheckBox showBox = new AjaxCheckBox("showBox", new Model<Boolean>(false)){
//onUpdate stuff
};
If you want to access the model value of the showBox
you can use getModelObject()
, which will return the boolean value stored in the AjaxCheckBox
's model.
Models are complex in wicket - but very powerful.
To illustrate further, Model
keeps its own reference to a value. PropertyModel
keeps a reference to a different object, and then stores the value in the property of that object. Look at the source code of Model
and you'll see it is very simple.
Upvotes: 4