2
我有一個部分需要一些相關的腳本,所以我想要追加這些JavaScript包含標籤的頭部,每當部分使用。所以我做:Rails部分︰使用content_for追加JavaScript包含標籤只有一次
# app/assets/javascripts/tricky_js.js.coffee
alert "I've been required!!"
# app/views/layouts/application.slim
head
# stuff...
= yield :head
body
= yield
# app/views/shared/_tricky_partial.slim
content_for :head do
= javascript_include_tag :tricky_js
p Bleh
# app/views/shared/unrelated_view.slim
= render 'shared/tricky_partial'
= render 'shared/tricky_partial'
= render 'shared/tricky_partial'
這當然會導致JavaScript代碼來追加3倍,因此運行alert("I've been required!!")
3倍。 如何追加腳本一次?