此代码在将 coffee_input 哈希传递给咖啡脚本代码的视图上正常工作
#if coffee_input?
if typeof coffee_input isnt "undefined"
mydata = JSON.parse coffee_input
for p in mydata by 1
if $('#'+p.name).length
.. do stuff
但在其他页面上,我一直在浏览器中出现此错误:
TypeError: 'undefined' is not an object (evaluating 'mydata.length')
虽然我使用的是 typeof coffee_input isntundefined
我不明白。
编辑:视图代码:
= javascript_tag do
window.coffee_input = '#{raw @js.to_json}';
@js
从控制器是:
@js = []
@js_hash = {"name" => p.name, "hosts" => "", "services" => ""}
@js << @js_hash