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

add option for leaving DOM untouched when calling destroy #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

balvig
Copy link

@balvig balvig commented Aug 15, 2019

Context

  • We are using Rails with Stimulus & Turbolinks , which uses JavaScript to ajax in new pages instead of refreshing the page when navigating a site.
  • That means that any instances and listeners are also persisted when moving to another page.
  • Rails also caches pages to speed up page loads, so when leaving a page the contents are cached, and upon revisiting it, those contents are loaded.

Problem

When using Colcade, this is the problem we run into:

  1. Colcade instances keep track of the element they are connected to using element.colcadeGUID.
  2. This GUID value gets removed when leaving a page with Turbolinks.
  3. The Colcade instance itself, however, sticks around.
  4. When we come back to the cached page, if we just initialize a new Colcade instance, we will have an ever increasing number of instances:
    Screen Shot 2019-06-20 at 16 40 27
  5. Colcade has a way of reusing existing instances, so I explored caching and resetting the colcadeGUID to make use of that like this:
disconnect() {
  this.data.set("colcateGUID", this.grid.colcadeGUID)
}

_initializeMasonry() {
  this.grid.colcateGUID = this.data.get("colcateGUID")
  this.colcade = new Colcade(...)
}
  1. However, the existing instance would be associated with columns no longer on the page, as they were removed by Turbolinks (and then added back in when the page is reopened). We would have to manually tell Colcade what the columns are again.
  2. I then thought, "ok, maybe we can just destroy the instance completely on stimulus#disconnect(), and start over when we come back to the page?".
  3. Colcade has a destroy method that does just that, but it also moves any items back to their non-masonry positions.
  4. This isn't visible in the browser but...it does appear to visible to the Turbolinks logic that remembers scroll position.
  5. That means, that just before leaving the page, to Turbolinks it looks like this:
    Screen Shot 2019-06-24 at 10 57 01
  6. Then, when going back, even if we re-initialize the masonry layout, the scroll position would always be slightly off:

Fix

Provide an option to destroy that allows us to remove instances/listeners, but leaves the DOM untouched (does not move items back outside the columns.)

Let me know if I'm completely misguided here or there is a better way! 🙏

@balvig balvig marked this pull request as ready for review August 15, 2019 15:55
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

Successfully merging this pull request may close these issues.

None yet

1 participant