nonzod
(Nicola)
1
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.
alex-w
(Pr0j3ct A1ex)
3
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');
...
}
});
nonzod
(Nicola)
4
I don’t know how to check.
Right, I had already checked this, thanks
alex-w
(Pr0j3ct A1ex)
5
I would just put an alert/console.log in the init function.
nonzod
(Nicola)
6
init is called only once.
Only the ajax call is made twice.
nonzod
(Nicola)
7
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