I have an event handler like so:

$(document).bind('cbox_closed', function(){
console.log( 'bind' );
});

I know the bind is not set twice, its the closed that is called twice.

Every time I close the colorbox I get this console log twice, and therefore any code within the bind gets implemented twice.

Is this expected or do I have a weird issue somewhere?

Comments

arosboro’s picture

I think this issue should be made in colorbox's github issue queue. I'm seeing 6 calls with colorbox_node, so I don't think this is specific to drupal, although it may be.

arosboro’s picture

Further investigation shows that this is caused by the behavior being attached multiple times.

I refactored my javascript to work like this:

(function ($) {
  Drupal.behaviors.slideShowColorboxControls = {
    attach: function (context, settings) {

      if (typeof Drupal.settings.slideShowColorboxControls !== 'undefined' &&
          typeof Drupal.settings.slideShowColorboxControls.init !== 'undefined') {
        return;
      }

      var eventHandler = function() {
        // place your custom logic here.
      }

      var handler = new eventHandler;

      $(document).on('cbox_closed', handler);
      $(document).on('cbox_open', handler);

      settings.slideShowColorboxControls = {};
      settings.slideShowColorboxControls.init = true;
    } 
  };
}(jQuery));
Neslee Canil Pinto’s picture

Issue summary: View changes
Status: Active » Closed (cannot reproduce)