Javascript 如何将这两段CoffeeScript组合成一个有效的文件?

Javascript 如何将这两段CoffeeScript组合成一个有效的文件?,javascript,ruby-on-rails,coffeescript,asset-pipeline,ruby-on-rails-5,Javascript,Ruby On Rails,Coffeescript,Asset Pipeline,Ruby On Rails 5,因此,我有两个独立工作的代码片段: $ -> $('[data-provider="summernote"]').each -> $(this).summernote height: 300 这是: $ -> companySelect = $('#company-select') jobFields = $('#job-fields') companySelect.change -> # if selected

因此,我有两个独立工作的代码片段:

$ ->
    $('[data-provider="summernote"]').each ->
      $(this).summernote
        height: 300
这是:

$ ->
  companySelect = $('#company-select')
  jobFields = $('#job-fields')
  companySelect.change ->
    # if selected option is the first option
    if $(this).find('option:selected').index() == 1
      jobFields.show().find(':input').prop 'disabled', false
    else
      jobFields.hide().find(':input').prop 'disabled', true
    return
  # call change immediately so this still works when already updating and not just creating.
  companySelect.change()
  return
考虑到这两个代码段控制着
Jobs#Views
中的两件事,我想在我的
Jobs.coffee
文件中将它们结合起来

但我不太确定如何将它们放在同一个文件中,这样它们就可以在不相互干扰的情况下工作

我尝试了一些东西,但由于我不完全理解咖啡脚本,我肯定我做错了,所以没有成功

编辑1

鉴于@Uzbekjon给出了他对这个问题的答案,我的组合文件现在看起来是这样的:

ready = ->
  $('data-provider="summernote"]').each ->
    $(this).summernote(height: 300)

$(document).ready(ready)
$(document).on('turbolinks:load', ready)


companySelect = $('#company-select')
  jobFields = $('#job-fields')
  companySelect.change ->
    # if selected option is the first option
    if $(this).find('option:selected').index() == 1
      jobFields.show().find(':input').prop 'disabled', false
    else
      jobFields.hide().find(':input').prop 'disabled', true
    return
  # call change immediately so this still works when already updating and not just creating.
  companySelect.change()
我现在得到这个错误:

ExecJS::RuntimeError at /jobs/new
SyntaxError: [stdin]:10:1: unexpected indentation

确保将文件命名为
jobs.js.coffee
(不要伪造
js
)。此外,间距/制表符也很重要,因此请确保不要缩进/取消缩进代码的一部分

$ ->
  $('[data-provider="summernote"]').each ->
    $(this).summernote
      height: 300

  companySelect = $('#company-select')
  jobFields = $('#job-fields')
  companySelect.change ->
    # if selected option is the first option
    if $(this).find('option:selected').index() == 1
      jobFields.show().find(':input').prop 'disabled', false
    else
      jobFields.hide().find(':input').prop 'disabled', true
    return
  # call change immediately so this still works when already updating and not just creating.
  companySelect.change()
编辑1: 根据您的编辑和评论。第10行和第11行出现了无意的缩进

jobFields = $('#job-fields')
companySelect.change ->
另外,您可能希望在
ready
回调中有这样的行为:

ready = ->
  # Setup "summernote" editor
  $('data-provider="summernote"]').each ->
    $(this).summernote(height: 300)

  # Company selector
  companySelect = $('#company-select')
  jobFields = $('#job-fields')
  companySelect.change ->
      # if selected option is the first option
      if $(this).find('option:selected').index() == 1
        jobFields.show().find(':input').prop 'disabled', false
      else
        jobFields.hide().find(':input').prop 'disabled', true
      return
    # call change immediately so this still works when already updating and not just creating.
    companySelect.change()

$(document).ready(ready)
$(document).on('turbolinks:load', ready)

好的,考虑一下你在我的另一个问题中的答案——组合咖啡脚本可能是什么样子的?我已经更新了这个问题,提供了更多关于我尝试过的东西和现在发生的事情的细节。完美。这正是我想要的。多谢孟!