我得到的错误令人费解,因为我从一个控制器/ views / javascript中复制了一些工作代码,使得名称发生了变化并取出了一些行以获得初始工作版本。导致运行时错误的coffeescript文件的revelvant部分如下所示。
应用程序/资产/ Javascript角/ companies.js.coffee
...
calculateResult = (company_id)->
data = $('#x_company_drill_interests').serialize()
console.log(" -01- in calculateResult ")
console.log data
$.ajax
url:"/companies/#{company_id}/projection.json",
type:"post"
dataType: 'json' # data type of response
data: data
failure: (data,success,xhr)->
console.log(" -01- in calculateResult - Failure ")
console.log data
success: (data,success,xhr)->
console.log("-01- in calculateResult - SUCCESS ")
company_listings_block = $ '#x_company_listings_results .infogroup-body'
head_row = $ '''
<tr>
<th>Company</th>
</tr>
'''
table = $ '<table border="0" cellpadding="0" cellspacing="5"></table>'
table
.addClass('info')
.append(head_row)
for result in data
name = result.display_name
result_row = $ """
<tr>
<td>#{name}</td>
</tr>
"""
table.append result_row
eval_result_block.html('').append table
产生错误:
未捕获的ReferenceError:未定义head_row
这是由行.append(head_row)
引起的。如果我删除此行,则会收到此错误Uncaught ReferenceError: eval_result_block is not defined
。
由于某些原因,我无法获得使用Post路由的代码。这是我添加修改路径文件的方法。
配置/ rutes.rb
resources :companies, only: [:destroy, :update] do
member do
get 'companies_drill_interests'
match 'projection', via: [:get,:post, :patch]
end
end
这里参考的是我从
复制的工作代码calculateResult = (drill_id)->
console.log(" -001- in calculate results")
data = $('#x_evaluation_assumption_params').serialize()
$.ajax
url:"/drills/#{drill_id}/projection.json",
type:"post"
dataType: 'json' # data type of response
data: data
failure: (data,success,xhr)->
console.log(" -001- in Calculate Result - Failure ")
console.log data
success: (data,success,xhr)->
# console.log(" -001- print data from call")
eval_result_block = $ '#x_id_evaluation_results .infogroup-body'
head_row = $ '''
<tr>
<th>Company</th>
<th>Price</th>
<th>Mkt Cap</th>
<th>Discovery Value</th>
<th>Target Price</th>
<th>Leverage</th>
<th>Risked Lev </th>
<th>Leverage with CFD's</th>
</tr>
'''
table = $ '<table border="0" cellpadding="0" cellspacing="5"></table>'
table
.addClass('info')
.append(head_row)
for result in data
if !(result.listing.option_unlisted)
name = result.display_name
share_price = '$' +
NumberHelpers.number_with_precision((result.listing.share_price/1000), {separator: '.', precision: 3, delimiter: ','})
market_capitalisation_mill = '$' +
NumberHelpers.number_with_precision((result.market_capitalisation/1000000), {separator: '.', precision: 1, delimiter: ','}) + 'M'
discovery_value = '$' +
NumberHelpers.number_with_precision(result.discovery_value_total, {separator: '.', precision: 0, delimiter: ','})
discovery_value_per_share = '$' +
NumberHelpers.number_with_precision((result.target_share_price), {separator: '.', precision: 2, delimiter: ','})
leverage =
NumberHelpers.number_with_precision(result.leverage, {separator: '.', precision: 0, delimiter: ','}) + '%'
risked_leverage =
NumberHelpers.number_with_precision(result.risked_leverage, {separator: '.', precision: 0, delimiter: ','}) + '%'
leverage_with_CFD =
NumberHelpers.number_with_precision(result.leverage_with_CFD, {separator: '.', precision: 0, delimiter: ','}) + '%'
result_row = $ """
<tr>
<td>#{name}</td>
<td>#{share_price}</td>
<td>#{market_capitalisation_mill}</td>
<td>#{discovery_value}</td>
<td>#{discovery_value_per_share}</td>
<td>#{leverage}</td>
<td>#{risked_leverage}</td>
<td>#{leverage_with_CFD}</td>
</tr>
"""
table.append result_row
eval_result_block.html('').append table
答案 0 :(得分:3)
在第一个代码中,eval_result_block
在使用之前未定义。
关于result_row
,代码的第一部分是for
循环,代码的另一部分是for
循环,因为risked_leverage
有一个不同的缩进,4个空格更短。这意味着它位于for
循环之外,因此,result_row
也在外面。
在coffeescript中,缩进问题。如果您放置3个空格而不是2个,则生成的代码将不同。看看你的代码:
failure: (data,success,xhr)->
console.log(" -01- in calculateResult - Failure ")
console.log data
success: (data,success,xhr)->
console.log("-01- in calculateResult - SUCCESS ")
company_listings_block = $ '#x_company_listings_results .infogroup-body'
head_row = $ '''
如果不同
failure: (data,success,xhr)->
console.log(" -01- in calculateResult - Failure ")
console.log data
success: (data,success,xhr)->
console.log("-01- in calculateResult - SUCCESS ")
company_listings_block = $ '#x_company_listings_results .infogroup-body'
head_row = $ '''
因为在第一种情况下有3个空格,而在第二种情况下有2个空格。
这里你的代码有正确的缩进。通过阅读您的代码,我认为result_row
和table.append result_row
必须在for
循环中。
calculateResult = (company_id)->
data = $('#x_company_drill_interests').serialize()
console.log(" -01- in calculateResult ")
console.log data
$.ajax
url:"/companies/#{company_id}/projection.json",
type:"post"
dataType: 'json' # data type of response
data: data
failure: (data,success,xhr)->
console.log(" -01- in calculateResult - Failure ")
console.log data
success: (data,success,xhr)->
console.log("-01- in calculateResult - SUCCESS ")
company_listings_block = $ '#x_company_listings_results .infogroup-body'
head_row = $ '''
<tr>
<th>Company</th>
</tr>
'''
table = $ '<table border="0" cellpadding="0" cellspacing="5"></table>'
table
.addClass('info')
.append(head_row)
for result in data
name = result.display_name
result_row = $ """
<tr>
<td>#{name}</td>
</tr>
"""
table.append result_row
company_listings_block.html('').append table
您可以尝试将每个代码部分编译成javascript,它将帮助您了解发生的情况。 Coffee compiler