views:

101

answers:

1

I create a button, when clicked, it activates a backup feature.
My problem, backup is launched before I have clicked to this button.
How, do I fix this problem ? Any Idea ?

Here is my code (fragment) :

(button) :

                var oSaveCuratedQuery = new YAHOO.widget.Button({
                type: "button",
                label: "Save Query",
                id: "updateCuratedQuery",
                name: "updateCuratedQuery",
                value:  "updateCuratedQueryValue",
                container: idReq });

YAHOO.util.Event.addListener("updateCuratedQuery-button", "click", saveCuratedQuery(idReq, contentCurValue));

(backup function) :

function saveCuratedQuery (geneId,curatedText) {

            var handleSuccessGeneQueries = function(o){
                Dom.get('progress').innerHTML = "Data Saved...";
            }

            var handleFailureGeneQueries = function(o){
               alert("Save failed...")
            }

            var callbackGeneQueries =
            {
              success:handleSuccessGeneQueries,
              failure: handleFailureGeneQueries
            };


   var sUrlUpdate = "save.html?";
   var postData = 'key=saveCuratedQuery&value=gene_id==' +geneId+ '--cq==' +curatedText;

 var request = YAHOO.util.Connect.asyncRequest('POST', sUrlUpdate, callbackGeneQueries, postData);

        }

I also try : oSaveCuratedQuery.on("click",saveCuratedQuery(idReq, contentCurValue));
But same problem !
The backup is done before I click "save" button.

Thank you for help.

+1  A: 

The third argument of addListener should be a function to be run when the event happens.

You are passing it the return value of saveCuratedQuery instead.

var callbackSaveCuratedQuery = function (idReq, contentCurValue) {
    return function callbackSaveCuratedQuery () {
        saveCuratedQuery(idReq, contentCurValue);
    };
}(idReq, contentCurValue); // Use an anonymous closure function
                           // to ensure that these vars
                           // do not change before the click
                           // event fires.

YAHOO.util.Event.addListener("updateCuratedQuery-button", 
                             "click", 
                             callbackSaveCuratedQuery
                            );

See https://developer.mozilla.org/en/A_re-introduction_to_JavaScript#Closures if you need to learn about closures.

David Dorward
David is on the money here. you are calling the function not pointing to it...
BigBlondeViking
Thank you very much, I have a backup button that works now !Also, thank you for link (many interesting things on this page).
Fabien Barbier