-
FEATURED COMPONENTS
First time here? Check out the FAQ!
In my ViewModel I have an object that represents what I'm capturing data from the form for. (For simplicity using Person here)
The problem is one of the properties is an int that I need to bind to a radiogroup, but ZK seems to want a String to decide what value to set?
<radiogroup id="age" selectedItem="@bind(vm.person.ageRangeId)"> <radio id="a1" label="10-18" value="1"></radio> <radio id="a2" label="19-25" value="2"></radio> </radiogroup>
The radio button won't be set if "ageRangeId" is an int. It works if I have it a String. The somewhat easy way out is to bind the selected item to a vm "ageRangeAsString" and then in the getter/setter I do the conversion from/to the ageRangeId of the Person object. Seems a bit awkward though. Curious why ZK can't try to force the conversion (simply throw an exception if it fails.)
Am I missing something or am I correct that the selectedItem only works off of a string (when working off the value)?
When you bind to vm.person.ageRangeId, ZK actually looks for vm.getPerson().getAgeRangeId() and vm.getPerson().setAgeRangeId(). What does that tell you? You control the getter return type and setter argument type. So, even though your ageRangeId may be int, you can always create additional methods, such as getAgeRangeIdStr() and setAgeRangeIdStr() methods. You would then @bind to vm.person.ageRangeIdStr . You don't need an actual property called ageRangeIdStr because ZK won't look for one. It will only look for the getter and setter methods I described. Make sense? Then, your get/setAgeRangeIdStr() methods can handle the conversion between int and String in communicating the ageRangeId value back and forth.
Hi, rickcr
Auto conversion from int to string should be a good for users. To deal with this problem, excepting adding a getter, you could implement a converter to convert int to string.
@rdgrimes Typically yes you have control over the domain objects and you could add a getItemAsString/setItemAsString type of accessor... but in my opinion that often pollutes your domain objects and sometimes you are using a jar of domain objects that you don't even have pure control of - think of another department at the company. Yes, you could extend the class or create a wrapper class (adapter pattern) but it seems like a bit of a hack just to set a property that happens to use a different data type.
This is often a valid knock on Java.. sure pros cons exist I know. But when you are working things like groovy you typically don't have to worry so much about these kinds of things. I often prefer my front end to be loosely typed but the backend end value objects more strongly typed. On the front end it should try to coerce what it can at least for some basic number types.
I didn't want to pollute the domain object with a get/setString accessor so I opted to put that accessor in the view model itself and the accessor then works with the underlying domain object in the view model. (It's about 10 lines of code including braces that I really think could be done away with.)
Hi,
I think the converter (http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/MVVM/Data_Binding/Converter ) is a way to keep your domain object clean and also fulfill your requirement.
Ha yea. Converter is way overkill imo just for coercing an int to a string:) If there is a place to submit feature requests this should be one of them.
For now I'll just stick to a prop on the viewModel itself that gets/sets the int from/on the domain object...
public String getProdValTypeString() { return String.valueOf(selectedBuyerBlock.getProdValType()); } public void setProdValTypeString(String prodValTypeString) { this.prodValTypeString = prodValTypeString; //set the int property on the domain model: this.selectedBuyerBlock.setProdValType(Integer.valueOf(prodValTypeString)); }
Hi,
another way is to bind radiogroup's model to an integer list and use template to create <radio>, bind radio's value to each object which is also a integer now. Then the selection will work correctly because ]vm.person.ageRangeId and each are both integer now.
<radiogroup id="age" selectedItem="@bind(vm.person.ageRangeId) model="@load(vm.ageList)"> <template name="model"> <radio label="@load(each)" value="@load(each)"></radio> </template> </radiogroup>
Asked: 2012-09-07 02:29:32 +0800
Seen: 518 times
Last updated: Aug 26 '21