Search code examples
grailsgsp

Grails: g:remotelink and one request only


I am using g:remotelink from Grails' tags to do one ajax request in a dropdown menu:

       <ul class="nav navbar-nav navbar-right">
            <li class="dropdown">
                <g:remoteLink class="dropdown-toggle" data-toggle="dropdown"
                    controller="mystuff" action="items" update="itemsListMenu">Items<b class="caret"></b></g:remoteLink>
                <ul id="itemsListMenu" class="dropdown-menu">
                </ul>
            </li>
        </ul>

it's working fine but I want to start a request only the first time the dropdown menu is pressed and not every time the menu is open or closed. I read on the Grails' remotelink docs that it is possible using onSuccess/onLoaded/onComplete/etc functions but I am not sure what is the best way to do it. Any idea?

UPDATE:

Following john smith suggestion I have added this:

<g:remoteLink id="myButton" onSuccess="jQuery('#myButton').attr('onclick',' ')" ... >

Solution

  • if you want to use the onsuccess event handler with style, and if youre including jquery here you go

    function killLink(element) {
         element.attr("onclick"," ");
    }
    

    This function will simply remove the onclick ajax handler that grails creates from the tag,

    your gsp would look like this

     <g:remoteLink class="dropdown-toggle" data-toggle="dropdown"
                        controller="mystuff" onsuccess="killLink(this)" action="items" update="itemsListMenu">Items<b class="caret"></b></g:remoteLink>