【发布时间】:2014-11-04 01:39:21
【问题描述】:
在我的 rails 应用程序中,似乎所有 .js.coffee 文件都被加载到每个页面,无论如何。我遇到的问题是正在加载locations.js.coffee 文件,然后在函数中返回一个空值,因为locations.js.coffee 文件正在查找的表在任何其他页面上都不存在不是位置页面。
注意:在每个文件中:users.js.coffee、locations.js.coffee 和campaigns.js.coffee 我分别放置了console.log("users.coffee loaded") 、console.log("locations.coffee loaded") 和console.log("campaigns.coffee loaded") 看看是否已经加载
例如,刷新位置页面会成功输出到控制台:
但冒险进入用户或活动页面将尝试加载文件 locations.js.coffee,但由于 locatrions.js.coffee 所依赖的 ID 为 #restaurantLocations 的表在用户或活动中不存在,因此结果错误是:
对于行:table.columns().eq(0).each (colIdx) 在文件 locations.js.coffee 中。
如何防止此错误或仅允许为正确的页面加载正确的文件并阻止其他页面加载?
(剩余的locations.js.coffee代码):
jQuery ->
console.log("locations.coffee loaded")
# Setup - add a text input to each footer cell
$("#restaurantLocations tfoot th").each ->
title = $("#restaurantLocations thead th").eq($(this).index()).text()
$(this).html "<input type=\"text\" placeholder=\"Search " + title + "\" />"
# DataTable
table = $("#restaurantLocations").DataTable()
# Apply the search
table.columns().eq(0).each (colIdx) ->
$("input", table.column(colIdx).footer()).on "keyup change", ->
table.column(colIdx).search(@value).draw()
# Hiding the id column, but for use for data manipulation
table.column( 0 ).visible( false )
# Allowing multi-select
$("#restaurantLocations tbody").on "click", "tr", ->
$(this).toggleClass "selected"
# Allowing deletion (works)
$("#deleteLocations").click ->
multiSelected = table.rows(".selected").data()
table.rows(".selected").remove().draw false
for locationSelected in multiSelected
id = locationSelected[0]
$.ajax({
url: "/locations/" + id, # Note: $.ajax setup works as setting /locations/(id number) will allow deletion
type: "post",
dataType: "json",
data: {"_method":"delete"}
})
# Conditional 'Select All' (works)
$("#selectAllLocations").click ->
table.$('tr', {"filter":"applied"}).addClass "selected"
# Deselecting all (works)
$("#deSelectAllLocations").click ->
table.$("tr").removeClass "selected"
【问题讨论】:
标签: jquery ruby-on-rails coffeescript asset-pipeline