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

Fix jQuery deprecation issue #447

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/assets/javascripts/cocoon.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,16 @@
}, timeout);
});


$(document).on("ready page:load turbolinks:load", function() {
var hideRemoveFields = function() {
$('.remove_fields.existing.destroyed').each(function(i, obj) {
var $this = $(this),
wrapper_class = $this.data('wrapper-class') || 'nested-fields';

$this.closest('.' + wrapper_class).hide();
});
});
}
$(hideRemoveFields); // On page load
$(document).on('page:load turbolinks:load', hideRemoveFields); // On custom load events

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmmmmm. I think this is not correct and I do not have a simple way to fix it. The "ready page:load" events are both replaced by the "turbolinks:load" event. So this works perfectly fine for backwards compatibility (if you use the new turbolinks). Indeed, if someone does not use turbolinks it will no longer be triggered. But someone using turbolinks 5 will, with your code, call hideRemoveFields twice.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @nathanvda

I'm not sure if I understood why it should not work, but I don't have much experience with Turbolinks. To my understanding:

$(hideRemoveFields); // On page load

^ This will get executed on page load once regardless if turbolinks is used or not.

$(document).on('page:load turbolinks:load', hideRemoveFields); // On custom load events

^ This will get executed only when Turbolinks are used (either old version or the new one). It will get executed on initial load and subsequent turbolink-loads of body content.

Thus when Turbolinks is used the hideRemoveFields code will get executed twice on initial load. However it should not have any side effects.

Actually maybe this could be even better implementation:

  $(function() {
    hideRemoveFields();
    $(document).on('page:load turbolinks:load', hideRemoveFields); // Turbolinks support
  });

This will work with all jQuery versions. It will also work with Turbolinks and the code will be executed once per page load (either initial or one initiated by Turbolinks).

Am I missing something?

})(jQuery);

Expand Down