Re-rendering specific js file with turbolinks enabled

前端 未结 1 1852
有刺的猬
有刺的猬 2020-12-15 13:18

I\'m working with Rails 4 and would like to use the built in Turbolink gem. I\'m happy with all it does except I\'d like it to reload one of the js

相关标签:
1条回答
  • 2020-12-15 13:55

    You can use 'page:load' event from turbolinks to connect an initialize function to it. It will be called always when turbolinks fully reaload your new page.

    function initialize() {
      //js code what you want run always
    }
    
    $(document).ready(initialize);
    $(document).on('page:load', initialize);
    

    If you use jQuery a lot, it is useful to install in your Gemfile

    gem 'jquery-turbolinks'
    

    and your application.js

    //= require jquery
    //= require jquery.turbolinks
    

    Turbolinks has two other useful events:

    page:fetch //when new page begin to download
    page:change //when the page has changed over
    
    0 讨论(0)
提交回复
热议问题