如何使此CoffeeScript对象工作
How can I make this CoffeeScript object work?
我正在学习在启用了Twitter Bootstrap
的Rails
v4.2应用程序中使用一个名为Bootbox
的小型库。
主页上有几个示例来测试一些功能。在这些示例中,它们引用了一个小型JS文件,该文件包含一个名为Example
的对象。
我将其转换为CoffeeScript,如下所示:
# 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
实际的Bootbox功能没有问题。然而,当我点击对话框中的按钮时,我可以在控制台中看到一个错误:
Uncaught TypeError: Example.show is not a function
我想,似乎很清楚,我已经用一个名为"show"的类方法设置了一个对象Example
。不过,我绝对不是JS/CoffeeScript专家,这也是我提出这个问题的原因。
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})
相关文章:
- 读取在具有IE的工作程序中生成的对象URL
- 我怎样才能让jQuery即使在尚未创建的对象上也能工作
- JSON.stringify没有'在更改的对象上无法按预期工作.如何处理
- 如何让我的正则表达式代码在我的dojo cellWidget对象上工作
- dropable的Over事件是't工作正常,在可拖动对象被拖放到贪婪的可拖动对象上并再次拖动后
- 地图如何在 Ecmascript 6 中使用对象键工作
- 了解 JavaScript 对象实例或工作流
- JavaScript 返回对象未按预期工作
- 如何让、要求和原型协同工作以在 javascript 中制作对象
- 谷歌应用程序脚本-正在尝试使用getUrl.TypeError:在对象工作表中找不到函数getUrl
- 一个对象怎么能像返回自身和另一个对象一样工作
- "键“;jQuery中的对象无法正常工作
- 对象引用的隐式传递是如何工作的
- dojo类对象的工作原理
- app.use(restify.bodyParser())无法工作,因为req对象为空
- 从服务器端将属性绑定到套接字对象;似乎不能正常工作
- 为什么mongoose.connect()在mongoose对象上没有作为方法出现时工作得很好
- 在 JavaScript 不工作对象中连接两个变量
- JS循环插件在IE11中不工作.对象不支持属性或方法循环
- Javascript 对象:如何创建包含工作对象的封装