如何从 JavaScript 和 Grails 访问变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3899107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How I access a variable from JavaScript and Grails?
提问by Mike Milkin
I have a Grailsvariable which is of type JASONList that is rendered in a template.
我有一个Grails变量,它是在模板中呈现的 JASONList 类型。
Is there a way to access this list from inside a JavaScript function?
有没有办法从 JavaScript 函数内部访问这个列表?
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...
假设我想要调整大小以适应屏幕上的所有对象。无需进行数据库调用并从 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}".
问题在于,由于某种原因,Grails gsp 不会将“${reportList}”呈现为列表。相反,它将其呈现为字符串“${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?
我可能认为这个问题完全错误,但是有没有办法调整这些对象的大小或通过 document.getElementById 或类似的东西来获取它们?
The $reportList is populated by POJOas JSON conversion...
$reportList 由POJO填充为 JSON 转换...
采纳答案by Mike Milkin
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.
我想出了我的问题。基本上,如果您在 Grails 中使用 POJO,Grails 作为 JSON 转换不是很聪明。它所做的只是对象上的 toString 而不是潜在地查看所有公共访问器等。
It is kind of disappointing, but basically I need to create the JSON conversion in the toString method of my POJO.
这有点令人失望,但基本上我需要在我的 POJO 的 toString 方法中创建 JSON 转换。
回答by Matt Ball
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 stringthat ${reportList}evaluates to.
Grails 变量只存在于服务器端。JavaScript 在浏览器(客户端)中运行。即在发送到浏览器中的一切都是一个字符串,因此,尽管你可以使用Grails的生成一段JavaScript一样window.onresize = resize("${reportList}"),浏览器将只能看到字符串是${reportList}计算结果为。
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.sizeor list.subList(), because the listvariable is no longer a list; it's just a string.
这意味着,如果您使用 Grails 将变量传递给resize()JavaScript 函数,则参数 ( list) 将永远只是一个字符串——您将无法访问诸如list.size或 之类的服务器端列表方法list.subList(),因为该list变量不再是一个列表; 它只是一个字符串。
回答by mschonaker
I don't know, but maybe Grails doesn't want to evaluate expressions inside scripttags. Dynamically generated scripts is not a very good practice.
我不知道,但也许 Grails 不想计算script标签内的表达式。动态生成的脚本不是一个很好的做法。
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>""" %>
回答by Gregg
I'm not sure why your ${reportList} is being rendered as ${reportList}, because when I do the following:
我不确定为什么您的 ${reportList} 被呈现为 ${reportList},因为当我执行以下操作时:
var t = "${taskList}";
I get the following in my HTML:
我在我的 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.
也就是说,您仍然会遇到问题,因为 JavaScript 不知道如何处理您的 reportList。如果它是纯 JSON,则需要对它进行 eval() 以将其转换为 JavaScript 对象。

