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

前端 未结 19 1882
忘了有多久
忘了有多久 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:49

    I usually do the following for my rails 4 projects:

    In application.js

    function onInit(callback){
        $(document).ready(callback);
        $(document).on('page:load', callback);
    }
    

    Then in the rest of the .js files, instead of using $(function (){}) I call onInit(function(){})

提交回复
热议问题