javascript - How to enable `sorttable.js` on Rails4 with turbolinks -


  • rails 4.2.0, turbolinks (2.5.3)
  • jquery-rails (4.0.3), jquery-ui-rails (5.0.3)
  • sorttable.js
  • app used on chrome, firefox, , ie

what should happen: whether redirect, render, or clicking new page in app, table class sorttable (i edited in original js) should sortable column heading.

what happens: on contacts page. hit refresh, , logger statements in js appear. plugin works expected. if navigate home, contacts, file still listed in page source, no logger statements appear. plugin not work.

our application.js reads such:

//= require jquery //= require jquery_ujs //= require jquery-ui //= require jquery_nested_form //= require bootstrap //= require sorttable //= require_directory . //= require autocomplete-rails //= require turbolinks //= require bootstrap-switch 

disabling turbolinks per-page or altogether not solution us. open using gems if file not work turbolinks; however, gem should add little code app. we'd rather not have classes dedicated choosing column sort. also, columns to-be-sorted not activerecord (like 2 attributes in 1 cell).

after debugging, figured out why turbolinks not sorttable.js. js file includes triggers (some based on browser type) initiate sorttable on document load. such event listener incompatible turbolinks. have done make work following.

  1. move vendor/assets/javascripts/sorttable.js app/views/shared/js/_sorttable.html.haml

  2. prefix new file :javascript (%script{languauge: "javascript"}), , indent following lines 2 spaces

  3. remove sorttable application.js

  4. at top of related pages, add following code block:

    = render 'shared/js/sorttable' %script   $(document).on("page:change", sorttable.init) 

for code examples , further explanation, please see this gist.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -