1

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

当用户单击任何按钮时,会显示一个菜单,选择某些内容后,他将被重定向到另一个页面。该页面由产品编号决定,沿着产品/: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 日志显示:

带有“警报”部分:

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)

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

4

1 回答 1

1

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

于 2014-12-24T04:15:39.330 回答