【发布时间】:2023-03-06 14:07:01
【问题描述】:
我注意到通过改变application.js 中调用资产文件的顺序,会改变实际加载的资产。
例如,我的application.js 文件如下所示:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require chosen-jquery
//= require dataTables/jquery.dataTables
//= require dataTables/bootstrap/2/jquery.dataTables.bootstrap
//= require bootstrap
//= require bootstrap-datepicker
//= require raphael
//= require morris
//= require turbolinks
//= require underscore
//= require gmaps/google
//= require 'locations'
//= require 'campaigns'
//= require 'users'
在当前状态下,当导航到位置或活动页面时,这些文件 locations.js.coffee 和 campaigns.js.coffee 已加载,但在用户页面中,来自 users.js.coffee 的信息未加载。
现在,如果我将 applications.js 中列出的最后三个排序方式更改为:
//= require 'users'
//= require 'locations'
//= require 'campaigns'
然后users.js.coffee 文件被加载,但locations.js.coffee 和campaigns.js.coffee 都没有加载。
为什么会这样?怎么解决?
更新:添加了用户、位置、活动、“.js.coffee”文件:
users.js.coffee:
jQuery ->
Morris.Line
element: 'campaignTrendChart'
data: [
{y: '2012', a: 100}
{y: '2011', a: 75}
{y: '2010', a: 50}
{y: '2009', a: 75}
{y: '2008', a: 50}
{y: '2007', a: 75}
{y: '2006', a: 100}
]
xkey: 'y'
ykeys: ['a']
labels: ['Series a']
return
locations.js.coffee:
jQuery ->
# Create the table object for all methods
table = $("#restaurantLocations").DataTable()
# For selecting a restaruant and removing them
$("#restaurantLocations tbody").on "click", "tr", ->
if $(this).hasClass("selected")
$(this).removeClass "selected"
else
table.$("tr.selected").removeClass "selected"
$(this).addClass "selected"
return
# Button to delete restaurants
$("#deleteRestaurant").click ->
table.row(".selected").remove().draw false
return
return
# 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 + "\" />"
return
# Apply the column specific filtering
table.columns().eq(0).each (colIdx) ->
$("input", table.column(colIdx).footer()).on "keyup change", ->
table.column(colIdx).search(@value).draw()
return
return
return
$("#restaurantLocations tbody").on "click", "tr", ->
$(this).toggleClass "selected"
return
$("#button").click ->
alert table.rows(".selected").data().length + " row(s) selected"
return
return
campaigns.js.coffee:
jQuery ->
$('#campaign_category_ids').chosen()
$('[data-behaviour~=datepicker]').datepicker();
# Google maps
handler = Gmaps.build("Google")
handler.buildMap
internal:
id: "multi_markers"
, ->
markers = handler.addMarkers([
{
lat: 43
lng: 3.5
}
{
lat: 45
lng: 4
}
{
lat: 47
lng: 3.5
}
{
lat: 49
lng: 4
}
{
lat: 51
lng: 3.5
}
])
handler.bounds.extendWith markers
handler.fitMapToBounds()
return
$('#selectRestaurantsForCampaign').DataTable()
# Create the table object for all methods
table = $("#selectRestaurantsForCampaign").DataTable()
# Add a text input to each footer cell
$("#selectRestaurantsForCampaign tfoot th").each ->
title = $("#restaurantLocations thead th").eq($(this).index()).text()
$(this).html "<input type=\"text\" placeholder=\"Search " + title + "\" />"
return
# Apply the column specific filtering
table.columns().eq(0).each (colIdx) ->
$("input", table.column(colIdx).footer()).on "keyup change", ->
table.column(colIdx).search(@value).draw()
return
return
return
# For selecting, will need to extend to select all
$("#selectRestaurantsForCampaign tbody").on "click", "tr", ->
$(this).toggleClass "selected"
return
# Will extend to select all.. to get all present filtered locations
$("#selectAll").click ->
alert table.rows(".selected").data().length + " row(s) selected"
return
return
【问题讨论】:
-
删除最后三个要求并将其添加为最后一行
//= require_tree .。告诉我会发生什么。 -
@AlejandroBabio 现在只有
campaigns.js.coffee文件加载/渲染,locations.js.coffee和users.js.coffee不会加载,脚本也不会在屏幕上渲染... -
请检查服务器日志和浏览器控制台寻找错误。
-
在您启动 rails 服务器的终端上,向上滚动直到找到类似
Started GET "/"的内容从此时开始并向下滚动以查找错误消息。在浏览器中按 F12。
标签: jquery ruby-on-rails coffeescript asset-pipeline