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

前端 未结 19 1599
忘了有多久
忘了有多久 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条回答
  •  梦毁少年i
    2020-11-21 07:47

    Found this in the Rails 4 documentation, similar to DemoZluk's solution but slightly shorter:

    $(document).on 'page:change', ->
      # Actions to do
    

    OR

    $(document).on('page:change', function () {
      // Actions to do
    });
    

    If you have external scripts that call $(document).ready() or if you can't be bothered rewriting all your existing JavaScript, then this gem allows you to keep using $(document).ready() with TurboLinks: https://github.com/kossnocorp/jquery.turbolinks

提交回复
热议问题