2

How do I map calls to a Grails controller from a Javascript method? I see a method using PHP, but not with grails:

function getSelected(checkList)
        {
            var idList = new Array();
            var loopCounter = 0;
            //find all the checked checkboxes
            jQuery("input[name=" + checkList + "]:checked").each
            (
              function()
              {
                //fill the array with the values
                idList[loopCounter] = jQuery(this).val();
                loopCounter += 1;
              }
            );

            //call here

        }

Edit:

${remoteFunction(controller:"person", action:"runThroughAll", params:"[ids:idList]")}
2
  • Ryan - Are you trying to make an Ajax call to a grails controller? Commented Mar 9, 2012 at 18:30
  • Yeah - Ultimately, I'd like to make a controller method call that renders a template from the JS method using the IdList. Commented Mar 9, 2012 at 18:33

1 Answer 1

6

So, I feel like there are sort of two things you're asking here. I'm going to tackle them both. First, how do you get the URL right for a call to a grails controller from JavaScript? In my GSP page (I do it in my main layout but whatever), I like to do this little trick:

<script>
myapp.url.root = "<g:resource dir='' file='' />" + "/";
</script>

That will give you the base root of your app wherever it's deployed. Then, you can build your URLs in JavaScript:

myurl = myapp.url.root + "path/to/controller"

Then make a jQuery ajax call using that url.

Then make sure that your controller is set up to respond to whatever URL pattern you've just expressed.

The second question appears to be, "how can I send back an HTML fragment"?

Inside the controller itself, take the parameters from the request, use it to figure out whatever you need, then render the gsp, passing in the model you've created. It will look something like this:

def show() {
   def data = [hypothesis : metadataService.getHypothesis(params.id) as JSON]
   render(view:"create", model:data)
}

Then in jQuery, your success handler will get as an argument the returned response, which you can then inspect/manipulate/add to the dom.

Hopefully all that made sense. If I glossed over something or didn't answer the question you were asking, let me know.

EDIT: For future reference, here is the javascript method rewritten which we arrived at in chat:

function getSelected(checkList){ 
var idList = $("input[name='" + checkList + "']:checked").map(function(){ return $(this).val(); }); 

$.ajax({ 
url: "/path/to/controller", 
type:"POST", 
data:{ids:JSON.stringify(idList)} 
success:mySuccessFunction 
}); 
}
Sign up to request clarification or add additional context in comments.

7 Comments

Let me know if you would like me to flesh out the jQuery AJAX call and the controller method a little more, or if you need me to show you what the GSP would be like.
Also, I think this is not the best pattern to use. I prefer to ask the Grails application for data and build my markup using jQuery, rather than asking the grails app for markup.
Let me try this out - This approach is weird, because I need to basically pull all selected checkboxes from a list, perform an action against those domain objects based on the ID, and then render. Not sure if there is another way around it, but I'll try this out - Thanks and keep you posted.
I still think that you can potentially manipulate those objects via a RESTful service and return back the data pertaining to those domain objects in a RESTful response,then manipulate the DOM based on the data. I can email you an example of what I mean if you'd like me to.
I don't see your email - but could I just do this in my JS code (see my initial question)
|

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.