{"id":12337,"date":"2014-03-23T16:45:44","date_gmt":"2014-03-23T11:15:44","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=12337"},"modified":"2014-03-23T16:45:44","modified_gmt":"2014-03-23T11:15:44","slug":"new-data-binding-features-in-grails-2-3","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/new-data-binding-features-in-grails-2-3\/","title":{"rendered":"New Data Binding features in Grails 2.3"},"content":{"rendered":"<p>We recently upgraded to Grails 2.3 and found some pleasant features in the rewritten Data Binding. This raised my interest to look further into what is new and found some interesting things that could be useful:<\/p>\n<ol>\n<li>We can create a global setting for the dataBinding of the dates using the following setting in Config:[java]grails.databinding.dateFormats = [&#8216;MMddyyyy&#8217;, &#8216;yyyy-MM-dd HH:mm:ss.S&#8217;, &quot;yyyy-MM-dd&#8217;T&#8217;hh:mm:ss&#8217;Z&#8217;&quot;][\/java]\n<p>The formats specified will be attempted in the order in which they are included in the List. The second and third format in the list above<br \/>\nare the default ones.<\/li>\n<li>It is possible that you may want to override these global settings. This can be done using the @BindingFormat(&#8216;MMddyyyy&#8217;) annotation as follows:[java]\n<p>import org.grails.databinding.BindingFormat<br \/>\nclass Person {<br \/>\n @BindingFormat(&#8216;MMddyyyy&#8217;)<br \/>\n Date birthDate<br \/>\n}[\/java]<\/p>\n<p>BindingFormat also supports localization, with the help of optional code attribute:<\/p>\n<p>[java]<\/p>\n<p>class Person {<br \/>\n @BindingFormat(code=&#8217;date.formats.birthdays&#8217;) \/\/ date.formats.birthdays are defined in message.properties<br \/>\n Date birthDate<br \/>\n }<\/p>\n<p>[\/java]<\/li>\n<li>\u00a0You can also write your own handler for the <strong>bindingFormat<\/strong>, by implementing <strong>org.grails.databinding.converters.FormattedValueConverter<\/strong> into a custom class and register the bean in Spring Application Context.<\/li>\n<li>There is also support for the <strong>Structured Data Binding Editors<\/strong> which help binding structured params to a property, i.e., we can integrate serveral pieces of data in<br \/>\nparams into a single value. The common example can be to create a date value from day, month and year as separate params. All you need to do is to<br \/>\nextend the <strong>org.grails.databinding.converters.AbstractStructuredBindingEditor<\/strong>, override the <strong>getPropertyValue<\/strong> and register the instance with<br \/>\nSpring Application Context.<\/li>\n<li>There is also support for the<strong> Data Binding Event Listeners<\/strong>. The simplest way to do this would be to extend <strong>org.grails.databinding.events.DataBindingListenerAdapter<\/strong><\/li>\n<li>And the last but not the least: How many times have you felt the need to access a bindData mehtod in a service (or for that matter have your own implementation of the bindData) . Now\u00a0you can do it simply by injecting &#8220;<strong>grailsWebDataBinder<\/strong>&#8221; in the service as:[java]\n<p>class NeedsBindDataService {<br \/>\n def grailsWebDataBinder<br \/>\n def needsBinding(Widget widget, Map data) {<br \/>\n grailsWebDataBinder.bind widget, data as SimpleMapDataBindingSource<br \/>\n }<br \/>\n}[\/java]<\/p>\n<p>All this is well documented <a title=\"here\" href=\"http:\/\/grails.org\/doc\/2.3.x\/guide\/theWebLayer.html#dataBinding\">here<\/a>.<\/li>\n<\/ol>\n<p>Apart from these, there are some other improvements and bug fixes. I found the following worth mentioning as they were related to the project I work on:<\/p>\n<ol>\n<li>\u00a0Earlier, to bind collections into the command objects was not that straight forward. We used to do something like this in command objects: [java]class SchoolCO{<br \/>\n List&lt;StudentCommand&gt; students = ListUtils.lazyList([], {new StudentCommand()} as Factory)<br \/>\n}[\/java]<\/p>\n<p>But, now with new dataBinding, all you need to do is to declare students:[java]<\/p>\n<p>class StudentCO{<br \/>\n List&lt;StudentCommand&gt; students<br \/>\n }[\/java]<\/li>\n<li>There also was a bug due to which <strong>bindable: false<\/strong> for collections was being ignored. Now, this has also been fixed.<\/li>\n<\/ol>\n<div>Hope you all find it useful.<\/div>\n<p>Cheers,<\/p>\n<p>Imran Mir<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We recently upgraded to Grails 2.3 and found some pleasant features in the rewritten Data Binding. This raised my interest to look further into what is new and found some interesting things that could be useful: We can create a global setting for the dataBinding of the dates using the following setting in Config:[java]grails.databinding.dateFormats = [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":24},"categories":[7],"tags":[664,1253],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12337"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=12337"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12337\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=12337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=12337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=12337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}