【问题标题】:Rails 4 Turbo-links Coffee script not loading [duplicate]Rails 4 Turbo-links Coffee脚本未加载[重复]
【发布时间】:2017-02-25 16:24:40
【问题描述】:

这似乎没有加载到我的侧边栏。我认为这与 rails 4 中的 Turbolinks 有关,因为如果我重新加载页面,它似乎可以工作。关于如何更改它以使其正常工作的任何想法?

jQuery ->
  category = $('#subject_category').html()
  $('#subject_product').change ->
    product = $('#subject_product :selected').text()
    escaped_product = product.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
    options = $(category).filter("optgroup[label='#{escaped_product}']").html()
    if options
      $('#subject_category').html(options)
      $('#subject_category').parent().show()
    else
      $('#subject_category').empty()
      $('#subject_category').parent().hide()

$ ->
  category = $('#sidebar .category_select').html()   
  options = $(category).filter("optgroup[label='Car']").html()
  if options
    $('#sidebar .category_select').html(options)
    $('#sidebar .category_select').parent().show()
  else
    $('#sidebar .category_select').empty()
    $('#sidebar .category_select').parent().hide()

【问题讨论】:

  • 我的问题是咖啡脚本语法,不确定其他帖子是否解决了我的请求。因为这些不是加载事件
  • $ -> jQuery -> 的快捷方式,这是 document ready 的快捷方式。这篇文章描述了 document ready 与 turbolinks 的不同之处,我相信这是你的问题。
  • 所以我应该用$(document).on "turbolinks:load", ->替换$ ->,这样对吗? jQuery -> 的捷径是什么?我是否也应该将jQuery -> 替换为$(document).on "turbolinks:load", ->

标签: jquery ruby-on-rails ruby-on-rails-4 coffeescript


【解决方案1】:
$(document).on "turbolinks:load", ->
alert "page has loaded!"

还有一颗宝石 - jquery.turbolinks gem。以防万一。适用于 4.2 及更高版本。下面我就不知道了。

【讨论】:

  • 我应该将jQuery ->$ -> 替换为$(document).on "turbolinks:load", -> 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 2013-09-17
  • 1970-01-01
  • 1970-01-01
  • 2015-05-07
  • 2016-07-06
相关资源
最近更新 更多