【问题标题】:Proper Ordering of Assets in applcation.jsapplication.js 中资产的正确排序
【发布时间】: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


【解决方案1】:

我想你和莫里斯有同样的问题。

使用这个:在 users.js.coffee 上

jQuery ->
  $('#campaignTrendChart').map ->
    Morris.Line
      ... # the rest of your code

如果找不到 html 元素,Morris 将失败。我用.each而不是.map尝试了这段代码,但我认为只有一个元素你必须使用这个。

在campaigns.js.coffee上

$('#multi_markers').map ->
   handler = Gmaps.build("Google")
   handler.buildMap
      ... # the rest of your code

我认为这可以解决问题。

Morris 和 Gmaps 在搜索时需要在页面上显示 html 元素。如果元素不存在,此解决方法可防止调用 Morris 或 Gmaps。

应索伦在聊天中的要求:

如果您有多个可与 Morris 一起使用的元素,您可以将它们与一个类(可能是 .morris_line)进行分组,并且每个元素都必须保存其 Morris 数据。您可以像这样进行某种类型的 DRY:

在视图中:

<%= content_tag(:div, class: 'chart_bars', id: "chart_person_#{@person.id}",
data: {bar: chart_data}) do %>

你的 js 必须是这样的:

$('.chart_bars').each ->
   Morris.bar 
     element: this.id
     data: $(this).data('bar')
     ... # rest of the params for morris

【讨论】:

  • 我试过了,但是users.js.coffee中的代码没有渲染
  • 东部时间下午 12 点聊一聊怎么样?
  • 对不起,这次我错过了你的邀请。今天下午 4 点,点击this,如果可以,如果不能点击。我会把消息留在那里。
  • 现在还不到 12 点,但我会继续聊天
  • 如果您需要更多详细信息,请通过聊天告诉我。
猜你喜欢
  • 2017-12-27
  • 2016-06-25
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多