1

I have a Grails variable which is of type JASONList that is rendered in a template.

Is there a way to access this list from inside a JavaScript function?

Let's say I want onresize to fit all the objects on the screen. Without making a database call and refetching the entire list from Ajax...

Let's say the template does something like this:

<g:each var="report" in="${reportList?.myArrayList}">
  <li style="display:inline; list-style:none;">
    <img src="  ${report?.img}">
  </li>
</g:each>
<script type="text/javascript">
    function resize(list) {
        if (list.size <givenSize) // Pseudocode
            list.subList() // Pseudocode
    }
    window.onresize = resize("${reportList}")
</script>

The problem with this is that for some reason Grails gsp does not render "${reportList}" as a list. Instead it renders it as the string "${reportList}".

I am probably thinking of this problem completely wrong, but is there a way to resize these objects or get them through document.getElementById or something of that nature?

The $reportList is populated by POJO as JSON conversion...

4 Answers 4

4

Grails variables only exist on the server side. JavaScript runs in the browser (client side). Everything that's sent to the browser is a string, so while you can use Grails to generate a piece of JavaScript like window.onresize = resize("${reportList}"), the browser will only see the string that ${reportList} evaluates to.

That means that, if you use Grails to pass a variable to the resize() JavaScript function, the parameter (list) will only ever be a string - you won't be able to access server-side list methods like list.size or list.subList(), because the list variable is no longer a list; it's just a string.

Sign up to request clarification or add additional context in comments.

3 Comments

Yea getting the sublist and size was just psudo code.. Its a JSON representation of the object any way... but since the GSP is rendered on the server the JSON object should be populated when sending it to the function right?
@user: I'm not actually familiar with Grails/GSP (JSP is more my thing) but as long as you're passing parsed JSON into the JavaScript function, you should be okay.
err Ah ok, Yea I think thats the disconnect, the reference to the ${reportList} is being sent in as the string representation and not the JSON object that it represents, Maybe I need to handle this on the serverside in variable backed by the controller ... ... ...
0

I don't know, but maybe Grails doesn't want to evaluate expressions inside script tags. Dynamically generated scripts is not a very good practice.

But until you find the exact cause, you could try something like this:

<g:each var="report" in="${reportList?.myArrayList}">
  <li style="display:inline; list-style:none;">
    <img src="  ${report?.img}">
  </li>
</g:each>
<%= """<script type=\"text/javascript\">
function resize(list){
  if(list.size <givenSize) //posudo code
     list.subList() // psudocode
}

window.onresize = resize(\"$reportList\")
</script>""" %>

Comments

0

I'm not sure why your ${reportList} is being rendered as ${reportList}, because when I do the following:

var t = "${taskList}";

I get the following in my HTML:

var t = "[com.wbr.highbar.Task : 1, com.wbr.highbar.Task : 4, com.wbr.highbar.Task : 2, com.wbr.highbar.Task : 5]";

That said, you're still going to have issues, because JavaScript will have no idea what to do with your reportList. If it is pure JSON, you would need to eval() it so that it gets turned into a JavaScript Object.

1 Comment

I figured out my problem, basically if you are using POJO in grails the grails as JASON conversion is not very smart all it does is a toString on the object instead of potentialy looking at all the public accessors ect ... (kinda disapointing but basicly i need to create the JSON convertion in the toString method of my POJO)
-1

I figured out my problem. Basically if you are using POJO in Grails the Grails as JSON conversion is not very smart. All it does is a toString on the object instead of potentially looking at all the public accessors, etc.

It is kind of disappointing, but basically I need to create the JSON conversion in the toString method of my POJO.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.