문제

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