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