Skip to content

Conversation

@dredupuika
Copy link

Remove event listener after it is executed

@dredupuika
Copy link
Author

Also fixed Rails caching problems for Turbolinks 5

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).

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)

@michelson
Copy link
Owner

Hi @dredupuika , thanks for this PR submission.
I've left you a couple of comments in the code,

best

@dredupuika
Copy link
Author

What if we do it this way?

@dredupuika
Copy link
Author

@michelson did you have time to check this commit?

@kreintjes
Copy link

Also fixed Rails caching problems for Turbolinks 5

I can confirm this works. @michelson would be nice to see this merged and released so we don't have to use a fork any longer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants