GRAILS : include javascript file that contains grails code from within a plugin - grails

I want to include a javascript file custom.js in my gsp from a plugin. My main app uses this plugin as a dependency. The custom.js javascript file contains a variable whose value is obtained by executing grails code within GString.
var root = "${request.contextPath}";
It has to be a separate file and if I try to include it inside my .gsp file using <script src="custom.js"></script> the grails code doesn't get executed. If I use <g:javascript src="custom.js" /> it tries to fetch the file from within the plugin and cannot find it. The file is actually in my main application. But the code that's trying to include it is in the plugin. Something like a way of providing custom Javascript implementation. How do I go about this?

Update
Taking this answer into consideration I have to specify the value of the variable in a separate scrip tag. No grails code is interpreted if it's in a non-.gsp file.
I solved it. All I had to do was: add contextPath="" into the g:javascript tag
According to the documentation of the tag g:javascript about the contextPath attribute:
contextPath (optional) - the context path to use (relative to the
application context path). Defaults to "" or path to the plugin for a
plugin view or template.
It defaults to the path to the plugin. So by setting it to empty string we tell it to avoid the plugin path from the source. Previously it modified the paths to the javascript to something like appname/plugins/pluginname/js/custom.js. Now with contextPath set to empty string it simply makes the path appname/js/cusomt.js

Related

Generate URL of resources that are handled by Grails AssetPipeline

I need to access a local JSON file. Since Grails 2.4 implements the AssetPipeline plugin by default, I saved my local JSON file at:
/grails-app/assets/javascript/vendor/me/json/local.json
Now what I need is to generate a URL to this JSON file, to be used as a function parameter on my JavaScript's $.getJSON() . I've tried using:
var URL.local = ""${ raw(asset.assetPath(src: "local.json")) }";
but it generates an invalid link:
console.log(URL.local);
// prints /project/assets/local.json
// instead of /project/assets/vendor/me/json/local.json
I also encountered the same scenario with images that are handled by AssetPipeline1.9.9— that are supposed to be inserted dynamically on the page. How can I generate the URL pointing this resource? I know, I can always provide a static String for the URL, but it seems there would be a more proper solution.
EDIT
I was asked if I could move the local JSON file directly under the assets/javascript root directory instead of placing it under a subdirectory to for an easier solution. I prefer not to, for organization purposes.
Have you tried asset.assetPath(src: "/me/json/local.json")
The assets plugin looks in all of the immediate children of assets/. Your local.json file would need to be placed in /project/assets/foo/ for your current code to pick it up.
Check out the relevant documentation here which contains an example.
The first level deep within the assets folder is simply used for organization purposes and can contain folders of any name you wish. File types also don't need to be in any specific folder. These folders are omitted from the URL mappings and relative path calculations.

grails elfinder plugin: use placeholder in rootDir

in my Grails project I'm using elfinder plugin to manage files and directories. I want to have a dynamic root directory, because I use the plugin for different folders.
The path of directory is like the following:
grails.plugin.elfinder.rootDir = "${userHome}/docm_patients_doc/{patientcf}/"
where patientcf is the id of an entity in my application. When I enter into the show.gsp page of that entity, I need to replace the patientcf with the related value.
How can I do it?
EDIT:
I've tried to modify the placeholder before the script and div that shows elfinder in gsp page, but I notice that the path is not modified. Maybe the gsp is not the place in which the placeholder can be modified...
I am author of elfinder plugin, though plugins isn't developed with multiple roots in mind.
You can try this. Plugin registers a spring bean with name elfinderFileManager which has a property with name ‘root’ which is path to your root directory. Try setting the root property at runtime. The bean can be injected in your controller/service and you can try changing the root property.

grails render tag: can't find file unless it's located in src/templates/scaffolding?

I tried to follow the example under "Shared Templates" here:
http://grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.2.3%20Views%20and%20Templates
But this just plain didn't work. The tag I used was:
<g:render template="/includes/mySearch"></g:render>
I created a directory under the views called "includes" and created a gsp file, with a very basic form in it, named mySearch.gsp.
However, grails reported being unable to find the file:
java.io.FileNotFoundException: C:\springsource\sts-2.8.1.RELEASE\grails-1.3.7\src\grails\templates\scaffolding_mySearch.gsp
According to the documentation: "In this case you can place them in the root views directory at grails-app/views or any subdirectory below that location and then with the template attribute use a / before the template name to indicate the relative template path."
It would appear that this is exactly what I did, but grails was not looking there? Any advice?
Many thanks,
Alexx
Template files need to be starting with an underscore. Therefore you need to rename your mySearch.gsp to _mySearch.gsp.

Grails g:link - include app name in resulting anchor href attribute

This GSP:
<g:link controller="book" action="show" id="5">Example</g:link>
results in this HTML:
Example
This is relative to the HTTP host. If your site is running at http://localhost:8080 that's fine.
During development, an app will more commonly be running at http://localhost:8080/appName.
In such cases, the above link will not work - it will result in an absolute URL of http://localhost:8080/book/show/5 instead of the required http://localhost:8080/appName/book/show/5
What changes are required of the above GSP for the app name to be present in the resulting anchor's href?
The configuration setting grails.app.context should be equal to the context where you want your application deployed. If it's not set, as in the default configuration, it defaults to your application name, like http://localhost:8080/appName. If you want to deploy your app in the root context (e.g. http://locahost:8080/), add this to your Config.groovy:
grails.app.context = "/"
If the context is properly set, the URLs generated by g:link tags will include the context before the controller name.
I found that the meta tag is very useful for getting information in GSP files.
For example, if you want your application name, you can get it like this:
<g:meta name="app.name"/>
You can get any property in your application.properties file like that.
And if you, like me, need to concatenate it to another value, here is my example. Remember that any tag can be used as a method without the g: namespace. For example:
<g:set var="help" value="http://localhost:8080/${meta(name:"app.name")}/help" />
Grails documentation about this is a little poor, but it is here.
For me the single best reason to use <g:link> is that it adds the context if there is one, or omits it if you're running at http://localhost:8080 or in prod at http://mycoolsite.com - it's trivial to just concatenate the parts together yourself otherwise.
The same goes for using g:resource with css, javascript, etc. - it lets you have one GSP that works regardless of what the context is (e.g. 'appName'), since it's resolved at runtime.
I think that is what grails.serverURL is for. You defined this config variable in the Config.groovy, check the configuration documentation for Grails for more details.
Hope this helps!
createLink tag includes your appname / context parameter automatically in the link.
Here is reference doc for it.

Grails: Getting current View name from within a Taglib

Is there any way to find out the current view (or gsp file) that is being evaluated?
RequestURI doesn't work due to URL Mappings and forwards.
I'm trying to retrieve resources dynamically based on the current GSP file. For example, if product/view.gsp is being executed, I want to include product/view.css and product/view.js (if they exist). The part that I'm having a problem with is getting the text "view"
A gsp page is compiled into a class derived from org.codehaus.groovy.grails.web.pages.GroovyPage. You can get the whole pathname of the gsp file with ${this.getGroovyPageFileName()}, or just ${groovyPageFileName}. You'll have to trim off the path information yourself.

Resources