Skip to content

Commit

Permalink
Guard against missing currentGallery element (#124)
Browse files Browse the repository at this point in the history
When an image is loaded,
it can preload the next/previous image with a callback.
It's possible that between when the decision to preload is made
and the `loadImage` call for preloading happens,
the current gallery has been modified and the index to preload
is missing, causing the preload callback to error.
  • Loading branch information
rgalanakis authored and feimosi committed Oct 25, 2016
1 parent 448dfbe commit 5278cb3
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/baguetteBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,11 @@

function loadImage(index, callback) {
var imageContainer = imagesElements[index];
if (typeof imageContainer === 'undefined') {
var galleryItem = currentGallery[index];

// Return if the index exceeds prepared images in the overlay
// or if the current gallery has been changed / closed
if (imageContainer === undefined || galleryItem === undefined) {
return;
}

Expand All @@ -485,7 +489,7 @@
}

// Get element reference, optional caption and source path
var imageElement = currentGallery[index].imageElement;
var imageElement = galleryItem.imageElement;
var thumbnailElement = imageElement.getElementsByTagName('img')[0];
var imageCaption = typeof options.captions === 'function' ?
options.captions.call(currentGallery, imageElement) :
Expand Down

0 comments on commit 5278cb3

Please sign in to comment.