带有“#{}”的Coffeescript使得rails很慢

时间:2013-07-01 19:00:05

标签: ruby-on-rails ruby coffeescript

我和我的朋友在一个视图中编写了一个部分编码:

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

但是当我们在我们的部分中使用"product/#{product.id}" coffeescript时,该网站变得如此缓慢,加载它超过7秒,但是当我们将product.id保存在隐藏文本上时,它被加载到不足0.5秒。

唯一的变化是这个!除了"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日志显示:

使用'alert'部分:

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)

正如我所说,我们创建了一个部分,可以获得本地人的“产品”对象,并在点击提交时使用它来生成链接。

1 个答案:

答案 0 :(得分:1)

https://github.com/paulnicholson/coffee-filter的“警告”一节下有一个关于此的说明,指出如果其中有红宝石插值,则不会缓存咖啡因编译。据推测,coffeescript的编译速度很慢。当我在haml模板中嵌入带插值的coffeescript时,我看到这种缓慢。