开发者

Grails - Using JS variable in remoteFunction for controller and action args

I want to call an Ajax function by using remoteFunction. But I'd like to set controller and action names with JS variables. Like this:

...
var id = 1;     
for (var i = 0; i开发者_C百科 < updater.slaves.length; i++) {
        var slave = updater.slaves[i];
        var ctrl = slave.data.controller;
        var action = slave.data.action;
        ${remoteFunction(controller: ctrl, action: action, params: "'id=' + id", onSuccess: "onSuccessLoadApplications(arguments)", onFailure: "error(arguments)")}             
    }

The problem is I can't retrieve the ctrl and action values (it's ok for id var).

So, is it possible to make dynamic controller and action args for remoteFunction ?

Thanks.


I don't think it's possible to use remoteFunction like that.

remoteFunction is a taglib, which will means it get processed at server side. At that stage, the javascript don't run, so you can't append string by '+'.

In short, the code inside remoteFunction must follow valid Groovy syntax. which means you can't put javascript in like the example code.

I think that to make things work, you must write your own javascript to do this ajax job.


Really quickly, without testing it out. It seems if "'id=' + id" works for the params argument, shouldn't just "ctrl" work for the controller?

Have you tried:

${remoteFunction(controller: "'' + ctrl", action: "'' + action", params: "'id=' + id", onSuccess: "onSuccessLoadApplications(arguments)", onFailure: "error(arguments)")}             

??

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜