您的位置:首页 > Web前端 > JavaScript

Rails Javascript 只在需要的页面中加载

2015-08-31 15:32 501 查看
  最近在项目中使用的一些Javascript编辑器比较大,然而只是在单个页面中使用到. 集成到Asset Pipline中增加了页面的加载的内容会比较大, 于是想到了按需加载.

  在Application Helper中添加方法:

  def render_javascripts

    if controller_name == 'controller' and action_name == 'action'

      @content = content_tag('script', nil, { src: '\js\bigfile.js' }, false

      @content << content_tag('script', nil, { src: '\js\bigfile2.js' }, false

    end

  end

  然后在Application.html.erb中添加即可.

  <%= render_javascripts %>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: