Rails 4: how to use $(document).ready() with turbo-links

前端 未结 19 1583
忘了有多久
忘了有多久 2020-11-21 06:52

I ran into an issue in my Rails 4 app while trying to organize JS files \"the rails way\". They were previously scattered across different views. I organized them into separ

19条回答
  •  遥遥无期
    2020-11-21 07:33

    I figured I'd leave this here for those upgrading to Turbolinks 5: the easiest way to fix your code is to go from:

    var ready;
    ready = function() {
      // Your JS here
    }
    $(document).ready(ready);
    $(document).on('page:load', ready)
    

    to:

    var ready;
    ready = function() {
      // Your JS here
    }
    $(document).on('turbolinks:load', ready);
    

    Reference: https://github.com/turbolinks/turbolinks/issues/9#issuecomment-184717346

提交回复
热议问题