1
我知道這個問題已經被問了好幾次了,但是所有的答案都讓我困惑,因爲我不確定如何讓coffeescript代碼編譯到指定的jquery代碼中。從插件外部訪問jquery/coffeescript插件的方法
這是我到目前爲止有:
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的東西。
@RicardoTomasi:還有jQuery-UI風格的$(x).tagbox('setCurrentTag','hello world')'。這與我所見過的標準方法最接近,避免了在保留鏈接的同時污染名稱空間。 – 2012-04-12 20:46:10
@ muistooshort是的,但他要求其他。這是一個混亂的方式:) – 2012-04-12 22:02:45