如何使这个CoffeeScript对象工作?

时间:2015-11-11 15:40:20

标签: javascript coffeescript

我正在学习在我的Twitter get("/",(request....)) 启用Bootbox v4.2应用程序中使用名为Bootstrap的小型库。

主页上有几个例子可以测试一些功能。在这些示例中,它们引用了一个包含名为Rails的对象的小型JS文件。

我将此转换为CoffeeScript,如下所示:

Example

实际的Bootbox功能没有问题。但是,当我单击对话框中的按钮时,我可以在控制台中看到错误:

# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

jQuery ->

  ###*
  # This tiny script just helps us demonstrate
  # what the various example callbacks are doing
  ###

  Example = ->
    'use strict'
    elem = undefined
    hideHandler = undefined
    that = {}

    that.init = (options) ->
      elem = $(options.selector)
      return

    that.show = (text) ->
      clearTimeout hideHandler
      elem.find('span').html text
      elem.delay(200).fadeIn().delay(4000).fadeOut()
      return

    that

  $("#search-link").click ->
    bootbox.dialog
      message: 'I am a custom dialog'
      title: 'Custom title'
      buttons:
        success:
          label: 'Success!'
          className: 'btn-success'
          callback: ->
            Example.show 'great success'
            return

我想,似乎很清楚,我已经使用名为" show"的类方法设置了一个对象Uncaught TypeError: Example.show is not a function 。我绝对不是JS / CoffeeScript专家,这就是我提问的原因。

1 个答案:

答案 0 :(得分:1)

  Example = (->
    'use strict'
    elem = undefined
    hideHandler = undefined
    that = {}

    that.init = (options) ->
      elem = $(options.selector)
      return

    that.show = (text) ->
      clearTimeout hideHandler
      elem.find('span').html text
      elem.delay(200).fadeIn().delay(4000).fadeOut()
      return

    that
 )()
 Example.init({selector: JQUERY_SELECTOR})