views:

1349

answers:

4

Hi all,

I'd like to split my views in Grails into 2 files a .gsp file and a .js file so that I get a cleaner Javascript separation from my views. So here's an example:

views/index.gsp
views/index.js
views/home/index.jsp
views/home/index.js

But when I simply add the index.js script reference like this:

<script src="index.js" type="text/javascript"></script>

all I get is a 404.

Does anyone knows how to deal with this?

A great benefit would be to have the ability to use view data inside the index.js file to produce the desired content.

Matthias.

A: 

Update 2:

Grails offer the possibility of hooking into the build lifecycle using custom events.

An event handler can be written which synchronises all javascript files under grails-app/views with the target folder of web-app/js.

Place the custom code in $PROJECT/scripts/Events.groovy. The PackagingEnd is a good target for the invocation, since it happens right after web.xml is generated.

eventPackagingEnd = {  ->
     // for each js file under grails-app/views move to web-app/js
}


Update

If you'd like the javascript files simply 'meshed' together, you can do that using symlinks, e.g.:

grails-app/views/view1/index.js -> webapp/js/view1/index.js

As far as I know there is no way of forcing grails to directly serve content which is outside of web-app.

Alternatively you can inline you javascript, but that can have performance implications.


Javascript files belong under web-app/js.

Then you can reference them using <g:javascript src="index.js" />.

Robert Munteanu
Yes,... I was kind of afraid someone will say it. And from my point of view it's exactly what I'd like to avoid. I hate to say it but having the same view in 2 distant parts of the application that don't really come together is not helpful.So.. any other ideas?
Matthias Hryniszak
See my update in the answer.
Robert Munteanu
Using symlinks is also not an option since not all operating systems (like Windows for example) have them :( I need a more generic solution to that.
Matthias Hryniszak
Additionally it'd be great to have the index.js processed like the index.gsp file which means that the index.js file could have some parts of it generated and not being completely static. I'll add that to the question
Matthias Hryniszak
See my update #2 for a cross-platform solution.
Robert Munteanu
Ok I see your point. That however has another drawback: every time the Javascript file changes the server needs to be restarted for the build process to take place. Well I guess it's just one of the things you can't do with Grails... Thanks for helping out though.
Matthias Hryniszak
+2  A: 

Actually, it should be perfectly possible to serve a JS file (or any other file type) as a GSP from your grails-app/views/ directory. The only thing you have to do, is define a suitable URL mapping for those GSPs, e.g.:

"/javascript/home/index"(view:'/home/index.js')

With this URL mapping, you can put your JS code into grails-app/views/home/index.js.gsp (note the trailing .gsp) and you can use any grails tags in your JS source. To ensure that your JS is delivered with the correct content type, you may want to place

<%@ page contentType="text/javascript"%>

at the beginning of your GSP.

Unfortunately, the createLink tag doesn't support link rewriting to views, but it should be easy to write your own tag to create those links.

Anyways, keep in mind that this won't have a very positive impact on your app's performance. It's usually better to have static JS files (and also serve them as static resources) while passing dynamic stuff as parameters to JS functions for example. This will also keep you from some headaches wrt. caching etc.

Daniel Rinser
That's more/less what I was looking for - thanks!
Matthias Hryniszak
A: 

The idea is good, but Grails has this directory structure for a reason. The view folder is intended for a certain artifact type (views)..

You could clone your view folder structure under web-inf, but that gives you more work as I guess the idea behind this is to keep related files close together for convenience reasons.

Even though I'm not to excited about storing Javascript together with the view I loved Robert's idea of hooking into the build process by using build events to copy javascript sources into the right directory! If you decide to go down that road you might as well compress the sources while you're at it. ShrinkSafe is popular library.

Kimble
A: 

I don't think you are allowed to access js inside views/

if you need to do that ... here is the trick

  1. create your js and rename it with myjs.gsp (use "") iniside _myjs.gsp type you js
... write down you js in here ...
  1. inside you gsp (for example: index.gsp, view.gsp, etc) type this tag to upload you js

nightingale2k1