Вопрос

I tried to store all event handler of an element, then unbind them, then do something and then bind again, but aparently the variable with the handlers gets modified to an empty object when i unbind the handlers

  var events = $._data(this.$element.get(0), 'events');
  console.log(events);    //here the variable events contains an object with the handlers
  this.$element.unbind();
  console.log(events);   //here the variable events contains an empty object

  //function to bind again the handlers
  $.each(events, function() {
    // iterate registered handler of original
    $.each(this, function() {
      $('#target').bind(this.type, this.handler);
    });
  });
Это было полезно?

Решение

You can use extend in deep mode to copy data in events:

var events = $._data(this.$element.get(0), 'events'),
    eventsCopy = $.extend(true, {}, events);
this.$element.unbind();
$.each(eventsCopy, function() {
    $.each(this, function() {
        $('#target').bind(this.type, this.handler);
    });
});
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top