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

destroy & re-initialize, ._init is not a function #807

Closed
nusoft opened this issue Jun 27, 2018 · 5 comments
Closed

destroy & re-initialize, ._init is not a function #807

nusoft opened this issue Jun 27, 2018 · 5 comments

Comments

@nusoft
Copy link

nusoft commented Jun 27, 2018

Hi, my website shows photos like this CodePen, but with photos from a remote source: https://codepen.io/desandro/pen/eRRQVo

Now I would like to add a button which will 'refresh' the InfiniteScroll instance, to show newly added photos from the remote source. I tried with $container.infiniteScroll('destroy') and reinitializing InfiniteScroll, but that gives an error Uncaught TypeError: o._init is not a function

Any ideas? Thanks in advance!

@desandro
Copy link
Member

Thanks for reporting this issue. Could you post the code you're using, where you're triggering destroy then initializing a new instance? Or a live URL, or even better a reduced test case? See Submitting Issues in the contributing guidelines.

@nusoft
Copy link
Author

nusoft commented Jul 2, 2018

Hi, thanks for your reply. I added my code to your pen (I tried 2 different methods): https://codepen.io/anon/pen/aKMzLa

@desandro
Copy link
Member

desandro commented Jul 4, 2018

Thanks for that. You should be able to re-initialize both Infinite Scroll and Masonry by first destroying both plugin instances

$(document).on("click", ".reload", function(e) {
  $grid.infiniteScroll('destroy');
  $grid.masonry('destroy');
  initMasonry();
});

See demo https://codepen.io/desandro/pen/ab2cc8162dde24c18ff12caf360d6186

However, there currently is a bug that doesn't properly remove jQuery data on destroy. I've added a fix for this that will be in the next release. Until then, you can this duck punch

// duck punch
var destroy = InfiniteScroll.prototype.destroy;
InfiniteScroll.prototype.destroy = function() {
  destroy.apply( this, arguments );
  // remove jQuery data
  if ( jQuery && this.$element ) {
    jQuery.removeData( this.element, 'infiniteScroll' );
  }
};

desandro added a commit that referenced this issue Jul 4, 2018
remove jQuery data on destroy
@nusoft
Copy link
Author

nusoft commented Jul 5, 2018

Awesome! Thanks a lot!

@nusoft nusoft closed this as completed Jul 5, 2018
@desandro desandro changed the title Refresh InfiniteScroll + Masonry + remote source destroy & re-initialize, ._init is not a function Jul 5, 2018
@desandro
Copy link
Member

Infinite Scroll v3.0.5 has been released.

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

No branches or pull requests

2 participants