{"id":219,"date":"2009-08-09T19:57:43","date_gmt":"2009-08-09T18:57:43","guid":{"rendered":"http:\/\/blog.pczone.be\/?p=219"},"modified":"2009-08-09T19:58:31","modified_gmt":"2009-08-09T18:58:31","slug":"grails-jquery","status":"publish","type":"post","link":"http:\/\/blog.pczone.be\/?p=219","title":{"rendered":"Grails + JQuery"},"content":{"rendered":"<p>Today I had a bit of time to do some further grails fooling around. I already had a working application old school (so everything you do was a page refresh.). I wanted to do some small Ajax stuff (getting a product using it&#8217;s id and then prefilling the price fields with that value.)<\/p>\n<p>In my domain I have the following object<br \/>\n<code><br \/>\nclass Product {<\/p>\n<p>    String name<br \/>\n    String description<br \/>\n    double buyInPrice<br \/>\n    double unitPrice<br \/>\n    double vatPerc<\/p>\n<p>    static hasMany = ['productPricings':ProductPricingHistory]<\/p>\n<p>    String toString(){<br \/>\n        return \"${description} ${unitPrice}\"<br \/>\n    }<br \/>\n}<br \/>\n<\/code><\/p>\n<p>In my controller I have to following<br \/>\n<code><br \/>\nimport grails.converters.JSON<\/p>\n<p>class ProductController {<\/p>\n<p>   def scaffold = Product<\/p>\n<p>def showjson  = {<br \/>\n        def product = Product.get(params.id)<br \/>\n\trender product as JSON<br \/>\n     }<\/p>\n<p>}<br \/>\n<\/code><\/p>\n<p>In my gsp I have to following snippit<br \/>\n<code><\/p>\n<p>                $(document).ready(function(){<br \/>\n                        $(\"#product\\\\.id\").change( function() {<br \/>\n                                setPrice($(this).val());<br \/>\n                        });<br \/>\n\t\t\t$(\"#units\").change(function(){<br \/>\n\t\t\t\tcalculatePrice();<br \/>\n\t\t\t});<br \/>\n                });<br \/>\n\t\tfunction setPrice(productId){<br \/>\n\t\t\t$.ajax({<br \/>\n\t\t\t\turl:\"..\/product\/showjson\/\"+productId,<br \/>\n\t\t\t\tdataType:\"json\",<br \/>\n\t\t\t\tsuccess:function(json){initProduct(json);},<br \/>\n\t\t\t\terror:function(xhr){<br \/>\n      \t\t\t\t\t\talert(\"Failed to save patient!: \" +<br \/>\n\t\t\t\t\t\txhr.status + \", \" + xhr.statusText );<br \/>\n\t\t\t\t}<\/p>\n<p>\t\t\t});<br \/>\n\t\t}<\/p>\n<p>\t\tfunction calculatePrice(){<br \/>\n\t\t\tvar unitprice = $(\"#unitprice\").val();<br \/>\n\t\t\tvar units = $(\"#units\").val();<br \/>\n\t\t\t$(\"#priceExcl\").val((unitprice * units));<br \/>\n\t\t}<\/p>\n<p>\t\tfunction initProduct(json){<br \/>\n\t\t\t$(\"#unitprice\").val(json.unitPrice);<br \/>\n\t\t}<\/p>\n<p><\/code><\/p>\n<p>As you can see it is really nice. Okay with Java + DWR this is also very simple, but here I have it in my controller and just say render &#8230; as JSON and grails does the rest. Actually I still have to look if Spring has some kinde of build in JSON renderer. Probably somebody already created this. But hey now I save and run (no need for start stop of my application server \ud83d\ude00 ) and in the end it&#8217;s still bytecode I&#8217;m using.<\/p>\n<p>I still have to do some grails testing where I generate my backend in JPA and include it in a jar. However the grails\/groovy part should still be reloadable, right? Anybody tried this yet?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I had a bit of time to do some further grails fooling around. I already had a working application old school (so everything you do was a page refresh.). I wanted to do some small Ajax stuff (getting a product using it&#8217;s id and then prefilling the price fields with that value.) In my &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/blog.pczone.be\/?p=219\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Grails + JQuery&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[4],"tags":[],"class_list":["post-219","post","type-post","status-publish","format-standard","hentry","category-informatics"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3U9nk-3x","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/blog.pczone.be\/index.php?rest_route=\/wp\/v2\/posts\/219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.pczone.be\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.pczone.be\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.pczone.be\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.pczone.be\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=219"}],"version-history":[{"count":2,"href":"http:\/\/blog.pczone.be\/index.php?rest_route=\/wp\/v2\/posts\/219\/revisions"}],"predecessor-version":[{"id":221,"href":"http:\/\/blog.pczone.be\/index.php?rest_route=\/wp\/v2\/posts\/219\/revisions\/221"}],"wp:attachment":[{"href":"http:\/\/blog.pczone.be\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.pczone.be\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.pczone.be\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}