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

Fixes #223 #224

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
9 changes: 5 additions & 4 deletions lib/lazy_high_charts/layout_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ def request_is_referrer?
defined?(request) && request.respond_to?(:headers) && request.headers["X-XHR-Referer"]
end

def request_turbolinks_5_tureferrer?
defined?(request) && request.respond_to?(:headers) && request.headers["Turbolinks-Referrer"]
def is_turbolinks_5?
Gem::Version.new(Turbolinks::VERSION) >= Gem::Version.new('5.0.0')
Copy link
Owner

Choose a reason for hiding this comment

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

Interesting approach. Question: what would happen if you have Turbolinks present in the project but not enabled in javascript ? I think in that case this solution will always return true

Copy link
Author

Choose a reason for hiding this comment

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

Didn't cross my mind. Then i'm at my wit's end with this. I guess there is no way to determine if rails has loaded turbolinks.js with certainty.
My best bet is that it is only fixable by manually passing an argument that switches the turbolinks wrapper on/off

Anyhow. I guess i can't help on this one then.
Will keep using this for my implementation for the time being though.

Copy link
Owner

Choose a reason for hiding this comment

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

maybe a mix of the two solutions ?
is_turbolinks_5? && request_turbolinks_5_tureferrer?

Choose a reason for hiding this comment

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

I agree with @michelson ... The solution that only checks the gem version causes problems when I'm rendering charts in a PDF, where I have JS enabled but Turbolinks is not enabled (it is on my application though)

Copy link
Author

Choose a reason for hiding this comment

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

that has been fixed long ago with this this commit

Choose a reason for hiding this comment

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

Well, my charts on PDFs are still not being rendered, don't know why... (they are rendered normally when using @michelsons master).

end

def options_collection_as_string object
Expand All @@ -106,11 +106,12 @@ def encapsulate_js(core_js)
#{js_end}
EOJS
# Turbolinks >= 5
elsif defined?(Turbolinks) && request_turbolinks_5_tureferrer?
elsif defined?(Turbolinks) && is_turbolinks_5?
js_output =<<-EOJS
#{js_start}
document.addEventListener("turbolinks:load", function() {
document.addEventListener("turbolinks:load", function(e) {
#{core_js}
e.target.removeEventListener(e.type, arguments.callee);
Copy link
Owner

Choose a reason for hiding this comment

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

interesting, can you explain to us what this code does. where is the arguments variable is defined ?

Copy link
Author

Choose a reason for hiding this comment

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

The arguments.callee property contains the currently executing function. doc

you can always use:
var f = function(){ document.removeEventListener('turbolinks:load', f, true); #{core_js} }; document.addEventListener('turbolinks:load', f, true);
does the same thing.

Choose a reason for hiding this comment

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

Any chance you can merge this fix without the rest of the PR? This fix is really important, but on the other hand other commits from this PR break my code (e.g. verifying how to encapsulate JS only based on the Turbolinks v5 gem being loaded into the application)

});
#{js_end}
EOJS
Expand Down