单击时未调用函数内的 Coffeescript 函数

Coffeescript function within function not being called on click

我确定这很简单,但我无法在单击事件时调用此函数。我尝试过各种化身,但无法很好地发挥作用。如果我在每个点击事件中移动 if/else 循环,它就可以正常工作。我只是想通过将循环移动到单个引用函数来使代码有点干。这是语法问题还是逻辑问题? - 提前致谢。

不起作用

$(".filter_option").click ->
      $('#submission_list').children(".item ").show()
      checkIfItemsEmpty()

$('#current_filters').on 'click', 'a.filter_remove', ->
      $('#submission_list').children(".item").hide()
      checkIfItemsEmpty()

checkIfItemsEmpty = () ->   
  if !$('.item').filter(':visible').length
      $('#no_results').show()
      console.log('The show command was triggered')
  else
      $('#no_results').hide()
      console.log('The hide command was triggered')

作品

$(".filter_option").click ->
      $('#submission_list').children(".item ").show()
      if !$('.item').filter(':visible').length
         $('#no_results').show()
         console.log('The show command was triggered')
      else
         $('#no_results').hide()
         console.log('The hide command was triggered')

$('#current_filters').on 'click', 'a.filter_remove', ->
      $('#submission_list').children(".item").hide()
      if !$('.item').filter(':visible').length
         $('#no_results').show()
         console.log('The show command was triggered')
      else
         $('#no_results').hide()
         console.log('The hide command was triggered')

事实证明这是一个简单的格式化案例,我在发布后两秒钟就意识到了!

似乎

checkIfItemsEmpty = () ->   
  if !$('.item').filter(':visible').length
      $('#no_results').show()
      console.log('The show command was triggered')
  else
      $('#no_results').hide()
      console.log('The hide command was triggered')

必须缩进超过 jQuery -> 才能工作。

所以....

jQuery ->
   $(".filter_option").click ->
      $('#submission_list').children(".item ").show()
      checkIfItemsEmpty()

   $('#current_filters').on 'click', 'a.filter_remove', ->
      $('#submission_list').children(".item").hide()
      checkIfItemsEmpty()

checkIfItemsEmpty = () ->   
  if !$('.item').filter(':visible').length
      $('#no_results').show()
      console.log('The show command was triggered')
  else
      $('#no_results').hide()
      console.log('The hide command was triggered')

现在就像做梦一样。