Question

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);
    });
  });
Was it helpful?

Solution

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);
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top