Monday, 19 November 2012

Groovy + Javascript == Grooscript 0.1

Last weekend Grooscript 0.1 was released. Grooscript is a library that compiles Groovy to Javascript using the AST tree as the source for the conversion.

There's been a while since I was waiting something like this landing in the Groovy scene. Why couldn't we  have a Javascript "compiler" to use it in the views as well?

Although Grooscript still needs time to be mature it's really promising. I have played with it a little bit and it's really funny the things  you can do in Groovy. The most important feature in version 0.1 is that you can call native javascript inside your methods. The syntax to do it is the following:

      @org.grooscript.GsNative def myMethod(parameter){/*
         //Javascript Code Here
      */}

This syntax has been chosen among others mainly because the lack of native method support in Groovy. We couldn't have used the word 'native' as GWT does when invoking javascript native methods.

By the time I'm writing this entry I have found one issue in the native support in Grooscript, and it seems that the workaround is pretty easy, you only need to add a dummy method at the end of your script to make the native support works.
      
      @org.grooscript.GsNative def myMethod(parameter){/*
         alert(parameter);
      */}

      @org.grooscript.GsNative def dummy(){/**/}


Because I really liked the project I started creating a Grails plugin to be able to use it any Grails project. It is not available yet in the Grails repository, but I guess it will be eventually. You can clone it, package it and install it in your Grails application, the code is at github.

The plugin enables you to declare Groovy scripts as static resources and compile those scripts to Javascript code.

Groovy files must have the suffix '.gs'. I still don´t know why files with '.groovy' suffix can´t be 'watched' (Grails doesn´t see any changes made to that files). In the meanwhile I opened an issue at JIRA (The more votes the faster it could be resolved :-) )

You must declare the dependency of the 'grooscript' module in those modules you're declaring any groovy scripts (I also have added here the jquery dependecy because I use it in the example).


modules = {
    myGroovyFiles{
      dependsOn 'jquery'
      dependsOn 'grooscript'

      resource url:'js/gs/GrooscriptNative.gs'
    }
}


In order to proof the native support of Grooscript I did a really simple example. I made a script that changes the Grails logo in your Grails application for the Grooscript logo. Here's the code of the GrooscriptNative.groovy


import org.grooscript.asts.*

class GrooscriptNative{ 
 
 @GsNative def changeImage(selector,image){/*
       $(selector).attr("src",image.url);
 */}   
 
     /* Workaround for an issue */
 @GsNative def dummy(){/* */}
}

class ImageHolder{
    String url
}

def selector = "#grailsLogo a img"
def image = new ImageHolder(url:"http://grooscript.org/img/logo.png")
 
new GrooscriptNative().changeImage(selector,image)

If you've installed the plugin properly, declared the ".gs" file in the resources file and on running the application you opened the browser you should see the following:


Cool!! isn't it?

You can get Grooscript from its site or getting the jar adding the public sonatype repository as an extra maven repository to your project.

42 comments:

  1. Ecorptrainings.com provides GROOVY in hyderabad with best faculties on real time projects. We give the best online trainingamong the GROOVY in Hyderabad. Classroom Training in Hyderabad India

    ReplyDelete
    Replies
    1. Great Article android based projects

      Java Training in Chennai Project Center in Chennai Java Training in Chennai projects for cse The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training Project Centers in Chennai

      Delete
  2. hi...Im student from Informatics engineering nice article,
    thanks for sharing :)

    ReplyDelete
  3. Excellent blog has been given.very well explanation about plugin enables.
    thanks for sharing this blog.
    oracle fusion procurement online training

    ReplyDelete
  4. Oracle fusion financials online training institute we have our branch over all the india.
    Oracle Fusion Cloud HCM Online Training in Hyderabad, Bangalore, Delhi, Chennai, Kolkata, Pune, Mumbai, Ahmedabad, Gurgon, Noida, India, Dubai, UAE, USA, Kuwait, UK, Singapore, Saudi Arabia, Canada, Oracle Fusion HCM Online Trainings


    Oracle fusion Financials Online Training

    Oracle Fusion Financials online Training

    ReplyDelete
  5. All given info was wonderful and it's very helpful for everyone. I read your post is very nice thank you.
    We Have Top Rated CALFRE Search Engine to search about all different courses in Al Karama, Dubai Locations. For Example Search a particular course like Oracle ASCP Training in Location Dubai Know More Click Here

    ReplyDelete
  6. Hi, Thanks for posting a great information of the your blog.
    DOT NET Training Institutes in Ameerpet

    ReplyDelete
  7. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
    Oracle Project Accounting Training in Hyderabad

    ReplyDelete
  8. This article is well written and quite informative.
    More articles should be written and you have just found a follower.and more visit.
    mainframe training in hyderabad

    ReplyDelete
  9. Nice information. I was searching for the same. It helped me a lot and saved my time.
    Oracle Fusion SCM Training in Hyderabad

    ReplyDelete
  10. Thanks for providing a useful article containing valuable information. start learning the best online software courses.

    Workday Online Training

    ReplyDelete
  11. Really excellent information and thank you for giving your valuable information

    Workday HCM Online Training

    ReplyDelete
  12. It's Incredibly grand. Really very helpful article , Thank you for sharing


    Workday Online Training

    ReplyDelete
  13. Very interesting blog Really excellent information and thank you for giving your valuable information

    Oracle Fusion HCM Online Training

    ReplyDelete
  14. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well.

    Oracle Fusion SCM Online Training

    ReplyDelete
  15. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    best workday studio online training

    ReplyDelete
  16. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    microservices online training
    best microservices online training
    top microservices online training

    ReplyDelete
  17. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    workday hcm online training
    best workday hcm online training
    top workday hcm online training

    ReplyDelete
  18. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously
    in their life, he/she can earn his living by doing blogging.Thank you for this article.
    best tibco sportfire online training

    ReplyDelete
  19. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously
    in their life, he/she can earn his living by doing blogging.Thank you for this article.
    tibco sportfire online training

    ReplyDelete
  20. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously
    in their life, he/she can earn his living by doing blogging.Thank you for this article.
    python online training

    ReplyDelete
  21. I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously
    in their life, he/she can earn his living by doing blogging.Thank you for this article.
    blueprism online training

    ReplyDelete
  22. Thanks for sharing such a wonderful information from this post. We are providing the best services click on below links to visit our website.
    Oracle Fusion HCM Training
    Workday Training
    Okta Training
    Palo Alto Training
    Adobe Analytics Training

    ReplyDelete
  23. thanks for sharing a nice post keep posting https://snowflakemasters.in/

    ReplyDelete
  24. There will be best course in Hyderabad, duck creek policy program,duck creek can earn a salary 130000 per annum.it is useful to students career in the field you have interest visit my website link.thanks youhttps://duckcreektraining.com/

    ReplyDelete
  25. An awesome blog for the freshers. Thanks for posting this information.
    ServiceNow Training
    ServiceNow Online Training

    ReplyDelete