Multi-mount turbocharged rails 4

There are similar questions to this, but they all seem to be triggered by the javascript tag in the body, in my case mine is in the head. I also cannot move my js outside the page changer (I don't think I can at least)

I follow the rails convention, having all my js in each file according to the resource and including them in application.js.

My js is in my layout file in my head:

<!DOCTYPE html> <html> <head> <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> 

My files follow the recommendations in the rails documentation for using page change events: http://guides.rubyonrails.org/working_with_javascript_in_rails.html#turbolinks

Here is an example js file (report.js - I use a coffee script)

 load_reports = -> table = $('#reports').DataTable() company_input_field = $('#rc-cn-st-input') company_input_field.on 'keyup change', -> table.column(2).search(@value).draw() return $(document).on('page:change', load_reports) 

All this works fine until I use the back button. As soon as I do this, I get an event 2 times for every click. How to fix this and what is an agreement?

In other cases, I moved js outside the page change block and did:

 $(document).on 'click', 'td.details-control', -> # do something 

But this does not seem practical in the above case, the jquery data table that I am referring to is on only one page and should only be configured when I am on this page.

I looked at .off () but couldn't figure out how to use it in this case, everything I tried seems to either delete the active listener or fail.

+7
jquery ruby-on-rails-4 turbolinks
source share
1 answer

Try using

 $(document).on('turbolinks:load', load_reports) 

instead

 $(document).on('page:change', load_reports). 

This is what worked for me, it only loaded the page once.

By the way, this is for rails 4.2 and above fooobar.com/questions/17445 / ...

+1
source share

All Articles