Using Rails 3.1, where do you put your “page specific” JavaScript code?

前端 未结 29 2080
一生所求
一生所求 2020-11-22 11:08

To my understanding, all of your JavaScript gets merged into 1 file. Rails does this by default when it adds //= require_tree . to the bottom of your appl

29条回答
  •  遥遥无期
    2020-11-22 11:34

    Following the lead from Ryan, here's what I have done-

    application.js.coffee

    $ ->
        view_method_name = $("body").data("view") + "_onload"
        eval("#{view_method_name}()") if eval("typeof #{view_method_name} == 'function'")
        view_action_method_name = $("body").data("view") + "_"+$("body").data("action")+"_onload"
        eval("#{view_action_method_name}()") if eval("typeof #{view_action_method_name} == 'function'")
    

    users.js.coffee (controller specific coffeescript,e.g controller:users, action:dashboard)

    window.users_dashboard_onload = () ->
        alert("controller action called")
    window.users_onload = () ->
        alert("controller called")
    

    application.html.haml

    %body{:data=>{:view=>controller.controller_name, :action=>controller.action_name}}
    

提交回复
热议问题