Search code examples
javascriptweb-servicesyui

Why is io request sent that many times?


I'm new to Yui but still plan on understanding it. Therefore, I need you ! I'm have a bit of a problem and haven't found any solution yet. Here is the plan :

I have a button and when I click on it, that button will send Json to a webservice. If it was sent successfully, I get a 'success' alert and 'failure' otherwise.

Here is the problem :

  • I click on the button once, I get 1 success alert (ok)
  • I click one more time, I get 2 success alerts (why two ??)
  • I click a third time and get 3 more success alerts...
  • I refresh the page, click the button and get 1 success alert

So why is that ? Why would it display twice the alert the second time ? Here is the Yui code I am using :

    YUI().use('io-base', 'json', 'event', 'querystring-stringify-simple', function (Y) {
    Y.one('#connectBut').on('tap', function (e){

        var jsonCreateUser = {
                "login": "01234",
                "password": "TestUser"
            },
            handleSuccess = function () {
                alert("success");
            },
            handleFailure = function () {
                alert("failure");
            },
            url = 'myServer';

        Y.on('io:success', handleSuccess);
        Y.on('io:failure', handleFailure);

        Y.io(url, {
            method: 'POST',
            data: jsonCreateUser
        });
    });
});

IF that sounds any obvious to you, please explain me, really want to understand this one.

Thanks for the help !


Solution

  • Each tap event on #connectBut will bind handleSuccess to io:success.

    You must move the code binding handleSuccess to io:success out of the tap event handler.

    Code example.