Javascript Coffeescript jQCloud处理程序
我正试着在咖啡脚本中这样做 具体地说,我正在尝试将处理程序添加到jQWordCloud中,以获取所单击单词的标签 在我的咖啡脚本版本中Javascript Coffeescript jQCloud处理程序,javascript,coffeescript,visualization,Javascript,Coffeescript,Visualization,我正试着在咖啡脚本中这样做 具体地说,我正在尝试将处理程序添加到jQWordCloud中,以获取所单击单词的标签 在我的咖啡脚本版本中 while i < @counts.length x = @counts[i] @tag_list.push text: x.label weight: x.count handlers: click: -> temp = x -> alert "
while i < @counts.length
x = @counts[i]
@tag_list.push
text: x.label
weight: x.count
handlers:
click: ->
temp = x
->
alert "it worked for " + temp.label
()
++i
而i<@counts.length
x=@计数[i]
@tag_list.push
文本:x.label
重量:x.count
处理程序:
点击:->
温度=x
->
警报“它工作了”+温度标签
()
++我
我得到了一个意外的终止符错误,可能是因为(),但是如果您注意到在JSFIDLE上,删除它会破坏处理程序,解决此问题的常用CoffeeScript方法是使用: 当使用JavaScript循环生成函数时,通常会插入一个闭包包装器,以确保循环变量被关闭,并且所有生成的函数不只是共享最终值。CoffeeScript提供
do
关键字,该关键字立即调用传递的函数,转发任何参数
然后用一个普通的来表示。。。在
中,而不是while
循环,这样您就不必到处乱搞索引;更像这样:
for o in stuff
do (o) ->
tag_list.push
text: o.NAME
weight: o.COUNT
html:
title: "#{o.COUNT} varieties"
handlers:
click: -> console.log("it worked for", o)
tag_list = for o in stuff
do (o) ->
text: o.NAME
weight: o.COUNT
html:
title: "#{o.COUNT} varieties"
handlers:
click: -> console.log("it worked for", o)
演示:
或者您可以使用这样的循环理解:
for o in stuff
do (o) ->
tag_list.push
text: o.NAME
weight: o.COUNT
html:
title: "#{o.COUNT} varieties"
handlers:
click: -> console.log("it worked for", o)
tag_list = for o in stuff
do (o) ->
text: o.NAME
weight: o.COUNT
html:
title: "#{o.COUNT} varieties"
handlers:
click: -> console.log("it worked for", o)
并避免push
调用
演示:
顺便说一句,您可以通过在侧边栏的语言面板中选择CoffeeScript来使用jsfiddle.net上的CoffeeScript。我做了一些小修改,但这是一个完美的响应,谢谢!