-
Notifications
You must be signed in to change notification settings - Fork 154
Breaks when Turbolinks is enabled. #109
Comments
Looking into this now |
I'm not able to reproduce this locally, but I'm glad to revert e0a8446, since there's no added value to sticking JS at the bottom of the body if the document has no content. |
@searls This commit broke our test suite; we have code that does this: $('body').on('click', element, function() { ... }); to take advantage of jQuery's event delegation. Problem is, with this change, the code now runs before Placement of JS is not an incidental detail, and I think most people would expect it to be loaded after the body is ready. When I'm writing my own JS test pages (which I would normally do but I didn't configure this project), I always place scripts at the end of the document, as they typically would be in production. If this interacts badly with Turbolinks, I would argue this is a problem with Turbolinks (it causes numerous problems that it's unreasonable to expect all frontend tool authors to work around). |
Ugh, thanks @jcoglan for the clear description of the issue this raises for you. In all my years of jQuery'ing, however, I've always made a point to stick after event handlers (even those delegated off $(function(){
$('body').on('click', element, function() { ... });
}); Is not doing so intentional on your team's part? I like the idea of using jQuery selections before domready only slightly more than I like the idea of using turbolinks. |
^^^ This may be true in the general case, but it's absolutely not true in Rails, as the default Rails behaviour is to wrap up all your JS into Not sure what the ideal solution is here but I still think that including jasmine js files in the head makes much sense than in the body. |
Since this gem is rails-specific, I would suggest relying on the default Rails behavior. However, I believe there is a solution to accommodate both: the Script would still go in |
A couple thoughts at this point, @jcoglan (beyond our editorialization that generally binding in jQuery before
|
The |
Visiting the page
/specs
works fine the first time - but when I click any link (e.g. to run one specific spec instead of them) the page breaks with the Javascript errorUncaught Error: jquery-ujs has already been loaded!
.The problem is Turbolinks. Turbolinks reloads everything between the
<body>
tags without causing a full page refresh, so any<script>
tags within the body are going to be reloaded every time a link is clicked - causing bugs and errors like the one described here.As Turbolinks is now included in Rails by default, this is potentially going to be a time-wasting issue for a lot of people.
Solutions I see:
jasmine_js_files
(here) to preventturbolinks.js
from being included in the output script tags:/specs
only? I don't like the idea of removing turbolinks from thetest
gem group because someone might have a project where, for whatever reason, their integration tests are dependent on turbolinks being present.The text was updated successfully, but these errors were encountered: