Pergunta

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',' ')" ... >
Foi útil?

Solução

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>

Outras dicas

grails remoteLink create a JQuery Ajax request, so with jquery I would do something like this:

// first set loaded to false    
var loaded = false;

// if not loaded execute ajax request
if(!loaded){
    $.ajax({
        type:'POST',
        url:"/mystuff/items",
        success:function(data){
            $("#itemsListMenu").html(data);
        }
    }).done(function(){
        // when done set loaded to true. 
        loaded = true;

    });
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top