ashleydw / lightbox

A lightbox gallery plugin for Bootstrap
http://ashleydw.github.io/lightbox
MIT License
1.85k stars 1.3k forks source link

Use with turbolinks/ajax #303

Closed indianabenny closed 6 years ago

indianabenny commented 6 years ago

This is a "solved" issue. I simply wanted to post this here in case someone else ran into a similar issue.

I was running into an issue when using turbolinks with getting an ekkoLightbox function not defined error on pages called by turbolinks. After some messing around, I figured out I could tie the $.fn['ekkoLightbox'] script to the window object that persists even after turbolinks are loaded. Here's how I solved it:


$(document).ready(function () {
  window.ekkoLightbox = $.fn['ekkoLightbox'];

  $(document).on('click', '[data-toggle="lightbox"]', function (event) {
    event.preventDefault();

    if(window.ekkoLightbox) {
      $.fn['ekkoLightbox'] = window.ekkoLightbox
      $(this).ekkoLightbox({
        alwaysShowClose: true,
      });
    } else {
      window.ekkoLightbox = $.fn['ekkoLightbox']
      $(this).ekkoLightbox({
        alwaysShowClose: true,
      });
    }

  });

});