web icon indicating copy to clipboard operation
web copied to clipboard

Javascripts loading twice

Open navro opened this issue 9 years ago • 8 comments

Visit https://fsektionen.se/kalender or a page with a select2 controller. Navigate to another page. Press the back button in the browser. The calendar/select2 controller is now duplicated!

How do we fix this? Any idea @davidwessman? I love turbolinks 5...

navro avatar Jan 26 '17 14:01 navro

There are some fancy discussions in this Issue, regarding making the scripts idempotent.

For the calendar scripts I would say: $('#event_calendar').html(""); In the beginning of the initialisations should help!

I bet you could do something similar with Select2.

davidwessman avatar Jan 26 '17 22:01 davidwessman

You can add:

$(document).addEventListener("turbolinks:before-cache", function() {
  $('.select2').select2('destroy');
  $('.select2_tags').select2('destroy');
})

To the select2-loader and it should do the trick :). Based on:

  • https://github.com/turbolinks/turbolinks#preparing-the-page-to-be-cached
  • http://stackoverflow.com/questions/19328253/how-to-remove-select2-from-dom

davidwessman avatar Jan 26 '17 22:01 davidwessman

Might be nicer to solve the fullcalendar-issue the same way, but then you need another destroy method :)

davidwessman avatar Jan 26 '17 22:01 davidwessman

Got the calendar to work using $('#event_calendar').html("");, but not select2 😞

navro avatar Jan 27 '17 13:01 navro

@navro did you try the turbolinks:before-cache?

davidwessman avatar Jan 27 '17 14:01 davidwessman

@navro did you try the turbolinks:before-cache?

Yes, that's the one i tried

navro avatar Jan 27 '17 14:01 navro

I found the solution!

You should not use select2 as the classname for loading select2 on, then you get issues. https://github.com/aldeed/meteor-autoform-select2/issues/44#issuecomment-231216235

davidwessman avatar Oct 10 '17 19:10 davidwessman

We really should fix this little bug... Thanks for the solution @davidwessman, don't know why we haven't fixed it yet 😞

navro avatar Jun 04 '18 19:06 navro