【问题标题】:Coffeescript with "#{}" makes rails so slow带有“#{}”的Coffeescript使rails变得如此缓慢
【发布时间】:2014-12-24 12:15:39
【问题描述】:

我和我的朋友正在编写要在视图中调用的部分代码:

当用户点击任何按钮时,会显示一个菜单,选择某些内容后,他将被重定向到另一个页面。该页面由产品编号决定,产品编号位于 product/:id 旁边。

但是当我们在部分使用"product/#{product.id}"coffeescript 时,网站变得非常慢,加载它超过7s,但是当我们将product.id 保存在隐藏文本中时,它在不到0.5s 内加载.

唯一的变化就是这个!除了"product/#{product.id}" -> "product/" + ${#sth}.val()

这有什么原因吗?

-- 更新: 这是我用过的代码,我知道它现在很乱,因为我们改变了很多:但这就是想法

- if product.blank?
    - id = "new"
    - path = "#{new_software_path}/"
- else
    - id = product.id
    - path = product.getFileLink(:DASHBOARD_LINK)
.modal.hide.fade{:id => "role-options-#{id}","aria-hidden" => "true", "aria-labelledby" => "myModalLabel", :role => "dialog", :tabindex => "-1"}
    .modal-header
        %button.close{"aria-hidden" => "true", "data-dismiss" => "modal", :type => "button"} ×
        %h3#myModalLabel choose a language
    .modal-body.profile_body
        %input{type: :hidden, class: id, value: path}   
        %select{class: id}
            %option{value: :en} English
            %option{value: :de} German
    .modal-footer
        %button.btn{"aria-hidden" => "true", "data-dismiss" => "modal"}= t("forms.close")
        %button.btn.btn-primary{"data-id" => id}= t("forms.submit")
:coffeescript
    $(".btn-primary").click ->      
        $id = "." + $(this).data("id")
        window.location.href = $("input." + $id).val() + $("select." + $id).val()
        alert(#{product.id});

我的 rails 日志显示:

带有“警报”部分:

Started GET "/dashboard/softwares" for 127.0.0.1
Processing by SoftwaresController#manage as HTML
  Rendered shared/_otherlanguage.html.haml (141.5ms)
  Rendered shared/_otherlanguage.html.haml (150.0ms)
  Rendered shared/_otherlanguage.html.haml (149.3ms)
  ...
  Rendered shared/_otherlanguage.html.haml (139.8ms)
  Rendered softwares/manage.html.haml within layouts/dashboard (12869.5ms)
  Rendered layouts/_dashboard_navigation.html.haml (2.4ms)

Completed 200 OK in 12922ms (Views: 12890.0ms)

没有“警报”部分:

Started GET "/dashboard/softwares" for 127.0.0.1 at 2013-07-02 10:09:44 +0430
  Rendered shared/_otherlanguage.html.haml (145.6ms)
  Rendered shared/_otherlanguage.html.haml (3.8ms)
  Rendered shared/_otherlanguage.html.haml (0.9ms)
  Rendered shared/_otherlanguage.html.haml (0.8ms)
  ...
  Rendered shared/_otherlanguage.html.haml (0.8ms)
  Rendered softwares/manage.html.haml within layouts/dashboard (477.9ms)
  Rendered layouts/_dashboard_navigation.html.haml (0.9ms)

Completed 200 OK in 577ms (Views: 549.6ms)

正如我所说,我们创建了一个局部,它将为本地人获取一个“产品”对象,并在单击提交时使用它来生成链接。

【问题讨论】:

  • 不能用很少的信息来判断。最好发布更多代码。
  • "product/#{product.id}" 只是编译成 javascript "product/" + product.id,所以这没有多大意义。为什么你在两段代码之间使用不同的变量?
  • 这个product 变量来自哪里?它是如何进入你的 CoffeeScript 范围的?
  • 用代码和rails中的相关日志更新了我的问题。

标签: ruby-on-rails ruby coffeescript


【解决方案1】:

https://github.com/paulnicholson/coffee-filter 的“注意事项”部分下对此有一个注释,指出如果其中有 ruby​​ 插值,则不会缓存咖啡脚本编译。大概是coffeescript的编译速度很慢。当我在haml模板中嵌入带有插值的coffeescript时,我看到了这种缓慢。

【讨论】:

    猜你喜欢
    • 2017-02-18
    • 2014-06-10
    • 1970-01-01
    • 2015-10-06
    • 2014-11-04
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多