Enabling the AJAX Spinner in Grails (Christopher M Judd)

, , August 11th, 2008

Original Source

Christopher M Judd describes how to enable a simple ajaxy feature that every web 2.0 site needs theses days: a background operation feedback animation icon. Given that Grails is an open source project built on open source projects it is no surprise that it relies in the popular Prototype/Script.aculo.us pair to achieve this task. With all things Grails, getting this feature to work is pretty straight forward, as Christopher shows.

For standard page requests, browsers use an animated icon as an indicator to the user that the browser is waiting for the request to complete. However, for AJAX calls that are initiated via JavaScript in a separate thread the browser icon does not animate. So, typically the application developers use an animated icon within the page to notify the user the page is waiting for a request to complete. In an effort to simplify web development Grails automatically includes an animated spinner icon when using the Prototype or script.acolo.us AJAX frameworks.


To enable the Grails AJAX spinner you must include the Prototype or script.acolo.us JavaScript typically using the <g:javascript library=”prototype”></g:javascript> or <g:javascript library=”scriptaculous”></g:javascript>. But these declarations can not be included just any place within your page. These declarations must come before the tag since it includes the application.js file that registers the spinner with Prototype and requires Prototype variables to be available.

Christopher outlines the current options either using the standard layout (the available one when you generate views or use scaffolding) or when a custom layout is used. You may find the required GSP code at this link.

Tags: , ,

Validate Legacy Databases with Grails (Christopher M Judd)

, July 18th, 2008

Original Source

Christopher Judd, one of the three authors of Beginning Groovy and Grails: From Novice to Professional , has posted a piece of advice when working with legacy databases. You may find some of the same information in Scott’s article Mastering Grails: Grails and Legacy Databases , but Christopher mentions one configuration setting that has not been extensively documented as the other ones: validating your legacy database.

By default Grails assume you are creating a new database schema from scratch. But one of its strengths is it’s ease of mapping to existing database tables. However when you map to existing tables you must be careful. By default the DataSource.groovy file, is configured to create-drop tables so it is easy to inadvertently wipe out data and tables. In addition switching to the less disruptive option of update may not be safe either. See GORM objects by default have an id and version attribute that are injected by GORM. When GORM gets initialized it will add id and version columns to your database which you may not want or expect. So the best option may be to use the validate option which is not documented in the Grails user guide. On start up it will validate the tables have the columns that your domain classes expect and if not fail to start.

By the way, to turn off the version column you will need to set version to false in the domain class mappings. To prevent the id column from being created, you will need to map id to the primary key column of your table.

Tags: ,