ChriX
ChriX

Reputation: 961

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 < 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.

Upvotes: 1

Views: 2346

Answers (2)

Ho&#224;ng Long
Ho&#224;ng Long

Reputation: 10848

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.

Upvotes: 1

billjamesdev
billjamesdev

Reputation: 14642

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)")}             

??

Upvotes: 0

Related Questions