1

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The part "id" works depends on the way the taglib get processed, and it may changes. Simply says, the developers who write "remoteFunction" don't think about your case of changing controller name dynamically
0

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

??

2 Comments

I tried but it doesn't work. Grails doesn't replace the 'ctrl' and 'action' strings :(
hmmm, that seems odd, since the params "id" works. I'm changing it, see if it works now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.