Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closing PhotoSwipe with pswp.close() causes JS error #822

Open
andfinally opened this issue Apr 24, 2015 · 9 comments
Open

Closing PhotoSwipe with pswp.close() causes JS error #822

andfinally opened this issue Apr 24, 2015 · 9 comments

Comments

@andfinally
Copy link

When I close PhotoSwipe with a call to the close() method the close, unbindEvents and destroy events fire, but afterwards I get "Uncaught TypeError: Cannot read property 'gettingData' of null" from the second line of this function in core.js:

    _shout = function(name) {
        var listeners = _listeners[name];

        if(listeners) {
            var args = Array.prototype.slice.call(arguments);
            args.shift();

            for(var i = 0; i < listeners.length; i++) {
                listeners[i].apply(self, args);
            }
        }
    },

Is there any way I can suppress this error?

Here's an example http://codepen.io/andfinally/pen/wavZxp.

@dimsemenov
Copy link
Owner

No, there is no way to suppress the error, as you're destroying PhotoSwipe in the middle of a process. For now there are no plans to add extra checks to allow closing in events like beforeChange, you may use afterChange.

Perform your checks on arrows click by editing UI, or override prev/next functions, e.g.:

pswp.next = function() {
  if(something) {
        pswp.goTo( pswp.getCurrentIndex() + 1);
  }
}

@andfinally
Copy link
Author

Thanks Dmitry, unfortunately afterChange throws a similar error, "Uncaught TypeError: Cannot read property 'mainScrollAnimComplete' of null".

It never occurred to me I could override prev/next, very useful! Unfortunately I need to handle the drag move too - from the user's point of view a click on next is the same kind of event as a left drag to the next slide.

This is a problem for me, as I'm being asked to change the flow to make the gallery close at the end of the series. So if there are three images and the user launches the gallery at image 2, the swipe sequence is 2-3-1-close.

@andfinally
Copy link
Author

Dunno if it's any use to you Dmitry, but I've found I can fix this by just checking if _listeners is defined in _shout:

if (!_listeners) {
    return false;
}
var listeners = _listeners[name];

This seems like a small change that makes it possible to close the gallery from a beforeChange callback. I can do this as a pull request if you want.

(Thanks for a brilliant app by the way, it's extremely nicely done and very useful!)

@andfinally andfinally reopened this Apr 24, 2015
@dimsemenov
Copy link
Owner

Dunno if it's any use to you Dmitry, but I've found I can fix this by just checking if _listeners is defined in _shout:

It's a step, but it needs more testing – maybe something else should be changed, as memory leaks can appear if destroy is called during this action.

Send a pull request just to core.js with your changes, this checkup won't hurt anyway.

@andfinally
Copy link
Author

I see - thanks! will do.

@jacobschweitzer
Copy link

I was able to get a similar error:
Uncaught TypeError: Cannot read property 'beforeChange' of null

related to the listeners when doing the following -

Open a gallery, then close it.
Open it again, click the button on the far left which starts/stops the autoplay.

Check this codepen to reproduce the error - http://codepen.io/anon/pen/ZGoeQo

Autoplay works fine the first time you open it, and I can start/stop the autoplay with no issues. But when I open it the 2nd time then try to stop then start again I get errors and images disappear.

@dimsemenov
Copy link
Owner

@jacobschweitzer, you aren't clearing intervals and events after PhotoSwipe is closed.

@jacobschweitzer
Copy link

Well in my actual code I am doing this:

gallery.listen('close', function() {
clearInterval(window.photoswipeSlideshow);
});

and I tried this:
gallery.listen('destroy', function() {
clearInterval(window.photoswipeSlideshow);
});

but those didn't work. What do you mean about clearing events? Is there a way I can completely re-initialize Photoswipe when it opens? It seems like when it opens the 2nd time there is some things being carried over.

@jacobschweitzer
Copy link

Hey Dmitry, thank you for your help! You pointed me in the right direction and I figured it out just now 😅 , it was the click binding on the slideshow start / stop button . So now my close listener looks like this:

gallery.listen('close', function() {
clearInterval(window.photoswipeSlideshow);
jQuery( 'button.pswp__button--slideshow' ).off('click');
});

Thank you for making this great piece of JavaScript and helping those who use it! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants