我知道这个问题已经被问过好几次了,但是所有的答案都让我感到困惑,因为我不确定如何让咖啡脚本代码编译成指定的 jquery 代码。
这是我到目前为止所拥有的:
pluginName = 'tagbox'
states =
none: 0
typing: 1
altering: 2
defaults =
editing: true
tags: []
class Plugin
constructor: (@element, options) ->
@options = $.extend {}, defaults, options
@_defaults = defaults
@_states = states
@state = states.none
@_name = 'tagbox'
@currentTag = $("<div class='ui-individualtag'></div>")
# this is the public method I want
setCurrentTag: (tagText) ->
@currentTag.text(tagText)
$.fn[pluginName] = (options) ->
@each ->
if !$.data(this, "plugin_#{pluginName}")
$.data(@, "plugin_#{pluginName}", new Plugin(@, options))
)(jQuery, window, document)
然后在另一个脚本中,我希望能够像这样访问 setCurrentTag 方法:
tagbox = $('#tagbox').tagbox()
tagbox.setCurrentTag("hello world")
让我知道它是否有助于在 jquery 中显示它编译成的内容。