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
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 9 additions & 9 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ end
```

> *Rails 5 Note*: since rails 5 a `belongs_to` relation is by default required. While this absolutely makes sense, this also means
> associations have to be declared more explicitly.
> associations have to be declared more explicitly.
> When saving nested items, theoretically the parent is not yet saved on validation, so rails needs help to know
> the link between relations. There are two ways: either declare the `belongs_to` as `optional: false`, but the
> cleanest way is to specify the `inverse_of:` on the `has_many`. That is why we write : `has_many :tasks, inverse_of: :project`
> cleanest way is to specify the `inverse_of:` on the `has_many`. That is why we write : `has_many :tasks, inverse_of: :project`



Now we want a project form where we can add and remove tasks dynamically.
To do this, we need the fields for a new or existing `task` to be defined in a partial
Expand Down Expand Up @@ -389,7 +389,7 @@ This will either let you select an owner from the list of persons, or show the f
The callbacks can be added as follows:

```javascript
$(document).ready(function() {
$(function() {
$('#owner')
.on('cocoon:before-insert', function() {
$("#owner_from_list").hide();
Expand Down Expand Up @@ -442,7 +442,7 @@ The default insertion location is at the back of the current container. But we h
For example:

```javascript
$(document).ready(function() {
$(function() {
$("#owner a.add_fields").
data("association-insertion-method", 'before').
data("association-insertion-node", 'this');
Expand All @@ -453,12 +453,12 @@ The `association-insertion-node` will determine where to add it. You can choose

The `association-insertion-method` will determine where to add it in relation with the node. Any jQuery DOM Manipulation method can be set but we recommend sticking to any of the following: `before`, `after`, `append`, `prepend`. It is unknown at this time what others would do.

The `association-insertion-traversal` will allow node selection to be relative to the link.
The `association-insertion-traversal` will allow node selection to be relative to the link.

For example:

```javascript
$(document).ready(function() {
$(function() {
$("#owner a.add_fields").
data("association-insertion-method", 'append').
data("association-insertion-traversal", 'closest').
Expand Down Expand Up @@ -493,7 +493,7 @@ For example, suppose Task has many SubTasks in the [Example](#examples), and hav
Then this will do the thing.

```javascript
$(document).ready(function() {
$(function() {
$(".add_sub_task a").
data("association-insertion-method", 'append').
data("association-insertion-node", function(link){
Expand Down
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