Converter"

From Documentation
Line 50: Line 50:
 
</source>
 
</source>
 
* We retrieve "format" parameter's value  by <tt> ctx.getConverterArg("format") </tt>. This allows you to pass in arbitrary parameters.
 
* We retrieve "format" parameter's value  by <tt> ctx.getConverterArg("format") </tt>. This allows you to pass in arbitrary parameters.
 +
 +
 +
According to above code, we can pass a "format" parameter to 'formatedDate' converter.
 +
 +
<source lang="xml">
 +
 +
<label value="@load(item.creationDate) @converter('formatedDate', format='yyyy/MM/dd')"/>
 +
</source>
  
 
= Use Custom Converter =
 
= Use Custom Converter =

Revision as of 04:44, 9 February 2012

Converter performs two way conversion between ViewModel's property and UI component attribute value. It converts data to component attribute when loading ViewModel's property to components and converts back when saving to ViewModel. It's quite common requirement to display data in different format in different context. Using converter developers can achieve this without actually changing the real data in a ViewModel.


Implement a Converter

Developers can create a custom converter by implement Converter interface. The method coerceToUi() is invoked when loading ViewModel's property to component and its return type should correspond to bound component attribute's value. The coerceToBean() is invoked when saving. If you only need to one way conversion, you can leave unused method empty.

The following is how built-in converter 'formatedDate' implement.

public class DateFormatConverter implements Converter {
	/**
	 * Convert Date to String.
	 * @param val date to be converted
	 * @param comp associated component
	 * @param ctx bind context for associate Binding and extra parameter (e.g. format)
	 * @return the converted String
	 */
	public Object coerceToUi(Object val, Component comp, BindContext ctx) {
		//user sets format in annotation of binding or args when calling binder.addPropertyBinding()  
		final String format = (String) ctx.getConverterArg("format");
		if(format==null) throw new NullPointerException("format attribute not found");
		final Date date = (Date) val;
		return date == null ? null : new SimpleDateFormat(format).format(date);
	}
	
	/**
	 * Convert String to Date.
	 * @param val date in string form
	 * @param comp associated component
	 * @param ctx bind context for associate Binding and extra parameter (e.g. format)
	 * @return the converted Date
	 */
	public Object coerceToBean(Object val, Component comp, BindContext ctx) {
		final String format = (String) ctx.getConverterArg("format");
		if(format==null) throw new NullPointerException("format attribute not found");
		final String date = (String) val;
		try {
			return date == null ? null : new SimpleDateFormat(format).parse(date);
		} catch (ParseException e) {
			throw UiException.Aide.wrap(e);
		}
	}
}
  • We retrieve "format" parameter's value by ctx.getConverterArg("format") . This allows you to pass in arbitrary parameters.


According to above code, we can pass a "format" parameter to 'formatedDate' converter.

<label value="@load(item.creationDate) @converter('formatedDate', format='yyyy/MM/dd')"/>

Use Custom Converter

The most common way to apply a converter is to bind a component attribute to ViewModel's property which is a custom converter..

Return a converter as a property

public class MyViewModel{
	private Converter MyConverter = new MyConverter();

	public Converter getMyConverter(){
		return myConverter;
	}
}


Example to use custom converter

<label value="@load(vm.message) @converter(vm.myConverter)"/>


Use Built-in Converter

ZK have provided some built-in converters that are commonly used. They can be used by @converter('converterName', parameterKey='value') . Currently, built-in converter we provide are : formatedNumber, formatedDate

	<label value="@load(item.price) @converter('formatedNumber', format='###,##0.00')"/>
	<label value="@load(item.creationDate) @converter('formatedDate', format='yyyy/MM/dd')"/>
  • You should specify number or date pattern in format parameter's value for formatedNumber converter or formatedDate converter.



Version History

Last Update : 2012/02/09


Version Date Content
6.0.0 February 2012 The MVVM was introduced.




Last Update : 2012/02/09

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.