Jquery accumulating events

Asked

Viewed 73 times

1

I have a problem in a listing, is the following, I have a table, for each row has a link to be able to edit the line, with this, when clicking on this link opens a bootstrap modal with all its content coming via Ajax, and so far this is all right. And when I close the modal I call an event of it to be able to update the table line.

My problem is that you keep accumulating calls for this line update, for example: I have the first line, I will click the edit button, open the modal, and when I close this modal it updates me the data of the line clicked; when I click on another edit link (or even on it), open the modal and then when I close this modal it updates the data of the clicked line, only when looking at the console I see that it makes the first and the second call of the lines to be updated, I wonder what might be accumulating these calls that only happen when I close the modal?

It follows an idea of the code, what happens in this case is that every time you open the modal, when it closes it will be accumulating Alert

$('a.edit_row_dt').on('click', function(e) {
    e.preventDefault();

    var item_id = $(this).data('id');
    var row = $(this).closest('tr');

    $('#edit').load('edit.php', {"id" : item_id}, function(){
            $('#edit').modal();

            $('#edit').on('hidden.bs.modal', function(event) {
                event.preventDefault();
                alert(item_id); // aqui 
                $('.modal-dialog').remove();
            });
        }
    );
});
  • I haven’t been able to visualize exactly what event the problem is occurring in. Ideally, the event should be "scheduled" only once, but if you need a different behavior, you can always "Clear" previous events with: http://api.jquery.com/unbind/ before adding another one.

2 answers

2

Before adding an Handler to the event "Hidden.bs.modal", try to remove the previous Handler using the method off jQuery:

// Essa linha
$('#edit').off('hidden.bs.modal');

$('#edit').on('hidden.bs.modal', function(event) {
      event.preventDefault();
      alert(item_id); // aqui 
      $('.modal-dialog').remove();
 });
  • Both your answer and @abfurlan’s answer worked, give a question, which would be better?

  • 1

    It’s the same, according to documentation of the method .one(): "[...]The . one() method is identical to . on(), except that the Handler is Unbound after its first Invocation. [...] In other words, explicitly Calling . off() from Within a regularly-bound Handler has Exactly the same Effect."

2

Utilize .one() instead of .on() when closing the modal, when you use .on() it runs again every click.

$('#edit').one('hidden.bs.modal', function(event) {
    event.preventDefault();
    alert(item_id); // aqui 
    $('.modal-dialog').remove();
});

Jsfiddle

  • Both your answer and @Marcusvinicius' answer worked, give a question, which would be better?

  • 1

    @Marcelodiniz you’re right the two work, I prefer the .one() because it automatically triggers events, in addition to having to put less code :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.