在Grails的JavaScript源代码中执行groovy语句 [英] Executing groovy statements in JavaScript sources in Grails

查看:105
本文介绍了在Grails的JavaScript源代码中执行groovy语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上有2个地方可以在Grails中定义JavaScript函数,直接在GSP的元素中定义,并且在/ web-app / js(例如application.js)下的单独javascript源文件中定义。我们已经在application.js中定义了一个常用的javascript函数,但我们也需要能够使用groovy代码动态生成部分函数。不幸的是,$ {一些groovy代码}似乎没有在单独的javascript源文件中处理。



通过在脚本中定义javascript函数标签在GSP页面,还是有更通用的解决方案?很显然,我们可以在模板GSP文件中的脚本标签中定义javascript函数,这个模板可以被重用,但是为了让我们的JavaScript函数在一个地方(即外部javascript源文件)中一起定义,我们需要进行大量的推动。这也具有性能优势(JavaScript源文件通常仅由每个客户端的浏览器下载一次,而不是在他们访问的每个html页面的源内重新加载相同的javascript函数)。我玩弄了将功能分解为静态和动态片断的想法,将静态片断放入外部源,并将动态片放入模板GSP中,然后将它们粘合在一起,但这似乎是不必要的破解。 / b>

有什么想法?

(编辑:这听起来像是动态生成JavaScript函数部分的想法,然后下载一次,然后一遍又一遍地被客户使用,这是一个糟糕的主意,但是,动态这个部分只能改变一个星期或一个月,而只是稍微改变一次,大多数情况下我们只是想要这个即使只有一次,而不是硬编码)。

解决方案

一个简单的解决方案,让JavaScript不显眼是创建一个JavaScriptController,并通过将其添加到您的 UrlMappings.groovy 文件中来映射它的动作/ js / *:

 / js / $ action{
controller =ja vascript

然后为每个你想要的动态JS文件创建一个动作,包括在你的布局< HEAD>和presto中,你有一个JS文件,你可以插入Grails代码片段! :)



注意:我发现Grails中存在一个错误,它没有正确地将文件扩展名映射到内容类型,因此您需要在视图文件的顶部包含<%@ page contentType =text / javascript; UTF-8%>


There are essentially 2 places to define JavaScript functions in Grails, directly in a element on the GSP, and within a separate javascript source file under /web-app/js (for example, application.js). We have defined a commonly reused javascript function within application.js, but we also need to be able to generate parts of the function dynamically using groovy code. Unfortunately, ${some groovy code} does not appear to be processed within separate javascript source files.

Is the only way to do this by defining the javascript function within a script tag on a GSP page, or is there a more general solution? Obviously we could define the javascript function in a script tag within a template GSP file which would be reused, but there is a lot of push to keep our javascript functions defined all together in one place (i.e. the external javascript source file). This has performance benefits as well (the javascript source files are usually just downloaded once by each client's browser, instead of reloading the same javascript functions within the source of every html page they visit). I have toyed around with the idea of breaking the function up into static and dynamic pieces, putting the static ones in the external source and putting the dynamic ones in the template GSP, then gluing them together, but this seems like an unnecessary hack.

Any ideas?

(edit: It may sound like the idea of dynamically generating parts of a JavaScript function, which is then downloaded once and used over and over again by the client, would be a bad idea. However, the piece which is "dynamic" only changes perhaps once a week or month, and then only very slightly. Mostly we just want this piece generated off the database, even if only once, instead of hard coded.)

解决方案

An easy solution to keep your JavaScript unobtrusive is to create a JavaScriptController and map its actions "/js/*" by adding this to your UrlMappings.groovy file:

"/js/$action"{
  controller = "javascript"
}

then just create an action for each dynamic JS file you want, include in in your layout <HEAD>, and presto, you've got a JS file that you can insert Grails snippets into! :)

Note: I've found that there's currently a bug in Grails that doesn't map file extensions to content-types properly, so you'll need to include <%@ page contentType="text/javascript; UTF-8" %> at the top of your view files.

这篇关于在Grails的JavaScript源代码中执行groovy语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆