Grails RemoteFunction 参数语法 [英] Grails RemoteFunction params syntax

查看:20
本文介绍了Grails RemoteFunction 参数语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将几个参数传递给 grails 中的 remoteFunction,但我正在努力正确格式化它

I'm trying to pass a couple of parameters to a remoteFunction in grails but I'm struggling to format it correctly

我想传入页面上一段数据的值加上我刚刚选择的文本框的值,所以在我的 onblur 中,我有一些类似的东西:

I want to pass in the value of a piece of data on the page plus the value of the text box that I have just tabbed out of, so in my onblur I have something along the lines of :

onblur=${remoteFunction(action:'dave',  update:'pack'+it.id,
         params:[denom:document.getElementById(denomValue+${it.id}).value ,
         amount:this.value ])}

这不能编译 - 我也不能用不同数量的单引号和转义字符来进行任何排列..

This doesn't compile - neither do any permutations I can come up with with varying numbers of single quotes and escape characters ..

我认为真正让我感到困惑的是我并不真正理解我在这里试图创造什么.是否像使用 JSP 代码创建稍后执行的 JavaScript?何时计算此表达式 - 是在编译页面时 - 还是在调用 oblur 时是 a=it?

I think what is really stumping me is thatI don;t really understand what I am trying to create here. Is it like using JSP code to create JavaScript which iwill be later executed? When does this expression get evaluated - it is at the time the page is compiled - or is a=it at the time that oblur gets called?

非常感谢任何帮助.

推荐答案

您似乎混淆了服务器端代码和客户端代码.

It looks like you've mixed up server-side code with client-side code.

当页面被构建"以发送到客户端浏览器时,将评估 Grails 代码.

The Grails code will be evaluated when the page is being "built" to be sent to the client browser.

一旦页面被传送到浏览器,Javascript 代码就会被评估.

The Javascript code will be evaluated once the page has been delivered to the browser.

考虑到这一点,让我们来看看您的 onblur 分配:

With this in mind let's take a look at your onblur assignment:

onblur=${remoteFunction(
         action:'dave', 
         update:'pack'+it.id, 
         params: [denom: document.getElementById(denomValue+${it.id}).value, 
                  amount: this.value ])}

鉴于 ${remoteFunction...} 调用是一个 Grails 标记,它将在服务器上进行评估,生成一个固定字符串,然后发送到客户端.调用中的所有内容都必须是有效的 Groovy 代码.

Given the ${remoteFunction...} call is a Grails tag, it will be evaluated on the server, generate a fixed string, then be sent to the client. Everything inside the call must be valid Groovy code.

查看参数映射,您在 denom 值中添加了一些 Javascript, Groovy 代码中:

Look at the params map, you've added some Javascript in the denom value, inside the Groovy code:

document.getElementById(denomValue

然后您尝试从 Groovy 添加一个值

then you try to add a value from Groovy

+${it.id}

然后是一些Javascript

then some Javascript again

).value

Groovy 编译器将尝试将 Javascript 评估为 Groovy 代码并失败.

The Groovy compiler will try to evaluate the Javascript as Groovy code and fail.

如果您需要在 Javascript 中访问客户端参数,您需要自己处理 Javascript(而不是使用 remoteFunction 标记),例如处理远程调用:

If you need to access client-side parameters in Javascript you'll need to handle the Javascript yourself (and not use the remoteFunction tag), for example to handle the remote call:

var path=${createLink(action:'dave',
                      params: [amount:this.value])} 
           + "&denom=" 
           + document.getElementById(denomValue+${it.id}).value

您还需要使用 Javascript 自己处理远程响应以更新pack"元素.您可以随时查看 remoteFunction 调用生成的内容,将其复制到页面中并对其进行编辑以执行您想要的操作.

You'll also need to handle the remote response yourself using Javascript to update the 'pack' elements. You could always look at what the remoteFunction call generates, copy it into the page and edit it to do what you want.

HTH

这篇关于Grails RemoteFunction 参数语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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