$.ajax() Call Twice

I wrote a small javascript module to intercept the click on a normal link:




yii.custom = (function($) {

  var pub = {

    isActive: true,

    init: function() {

      $('.click-me:not(.processed)').click(function(e) {

        $(this).addClass('processed');

        e.preventDefault();

        var jqxhr = $.ajax("/index.php?r=" + $(this).attr('data-path'))

        .done(function(data) {

          alert(data.status);

        })

        .fail(function(jqXHR, textStatus) {

          console.log(jqXHR);

          console.log(textStatus);

        });

        return false;

      });

    },

  };


  // Private functions

  // ...


  return pub;

})(jQuery);


jQuery(document).ready(function() {

  yii.initModule(yii.custom);

});



As you can see I’ve tried to:

assign a class "processed" to not reprocess the click;

use preventDefault();

return "false" ;

The call, however, is done twice, I checked the source, but I could not understand why this happens.

What is the best way to make ajax calls?

thank you

[s]I suppose here’s your answer.

The trick is to use $form.data(‘yiiActiveForm’).validated check.[/s]

my bad, wrong answer.

Is the init function getting called twice somehow?

The processed check won’t work like that either, The event is assigned before the link ever has the class.




$('.click-me').click(function(e) {

    if (!$(this).hasClass('processed')) {

        $(this).addClass('processed');

        ...

    }

});



I don’t know how to check.

Right, I had already checked this, thanks

I would just put an alert/console.log in the init function.

init is called only once.

Only the ajax call is made twice.

My error, the module not need:




jQuery(document).ready(function() {

  yii.initModule(yii.custom);

});



because initModule is alredy called by yii!

in yii.js:

In this case the root is yii…

sorry