-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi, I'm using ZK 7.0.2, I'm just wondering If I could pass view model property to programmatically binder's converter arguments. let say I have this property in my View Model class :
private List<Person> personList;
// ... personList setter & getter
When I pass this personList to my custom converter argument by ZUL page, like below snipset, it just work fine (both of coerceToUi & coerceToBean method work fine).
<textbox value="@bind(fx.person) @converter('personConverter', list=vm.personList)" />
But when I used it by programmatically binder (the equivalent purpose). it reach the custom converter class, but just pass plain String object rather than invoked view model object.
String[] beforeCmds = {"saveAction"};
HashMap<String,Object> converterArgs = new HashMap<>();
converterArgs.put("list", "vm.personList");
Textbox txtPerson = new Textbox();
binder.addPropertyLoadBindings(txtPerson, "value", "fx.person",
null, null, null, "'personConverter'", converterArgs);
binder.addPropertySaveBindings(txtPerson, "value", "fx.person",
null, null, null, "'personConverter'", converterArgs, null, null);
binder.addPropertySaveBindings(txtPerson, "value", "fx.person",
beforeCmds, null, null, "'personConverter'", converterArgs, null, null);
In personConverter class, it received pass args by :
List<Person> list = (List<Person>) bc.getConverterArg("list");
Error appears because can't cast string object "vm.personList" to List<person>. Or indeed it can not be pass view model property by programmatically binder? Please kindly help me. Thank you.
There is a util class to parse the values as EL expressions:
e.g.
Map<String,String[]> args = new HashMap<String, String[]>();
args.put("list", new String[] {"vm.personList"});
HashMap<String,Object> parsedArgs =
BindEvaluatorXUtil.parseArgs(binder.getEvaluatorX(),args);
parsedArgs will then contain an ELExpression ${vm.personList} an not just the string "vm.personList"
simplified from AnnotateBinderHelper.parseConverter(comp, propName) (after preparing the HashMap<string, string[]>="" line="" 725="" calls="" <a="" href="http://grepcode.com/file/repo1.maven.org/maven2/org.zkoss.zk/zkbind/7.0.3/org/zkoss/bind/impl/BindEvaluatorXUtil.java#BindEvaluatorXUtil.parseArgs%28org.zkoss.bind.sys.BindEvaluatorX%2Cjava.util.Map%29">BindEvaluatorXUtil.parseArgs(eval, args))
Hi, Cor3000 thanks a lot for your code example. It have tried it & work fine so far :) Here's is my implementation:
Map<String, String[]> args = new HashMap<String, String[]>();
args.put("list", new String[]{"vm.personList"});
Map<String, Object> parsedArgs = BindEvaluatorXUtil.parseArgs(binder.getEvaluatorX(), args);
Textbox txtPerson = new Textbox();
binder.addPropertyLoadBindings(txtPerson, "value", "fx.person",
null, null, null, "'personConverter'", parsedArgs);
binder.addPropertySaveBindings(txtPerson, "value", "fx.person",
null, null, null, "'personConverter'", parsedArgs, null, null);
binder.addPropertySaveBindings(txtPerson, "value", "fx.person",
beforeCmds, null, null, "'personConverter'", parsedArgs, null, null);
Asked: 2015-05-24 05:19:22 +0800
Seen: 17 times
Last updated: May 25 '15
In the map, we put normally the data, as you "translate" the zul, vm.personList is actually the data and not a string. I think this should work : Make a new referenceBinding to a component, and pass that to the converter.
chillworld ( 2015-05-24 08:34:14 +0800 )editHi Chillworld, thanks to your response. ok, I will tried it. and for additional information, it work fine when I pass the List collection not the VM Property String. so: converterArgs.put("list", getPersonList()) is work rather than converterArgs.put("list", "vm.personList").
ghostomg ( 2015-05-25 02:48:38 +0800 )edityes that works initially, but once the vm.personList is replaced by another list it will not reload, my suggestion below will reload in such a case if NotifyChange is triggered: so it depends a bit on your requirements
cor3000 ( 2015-05-25 03:26:18 +0800 )edit