我有这个函数,在事件发生时手动调用,例如click
,我需要在每次单击时调用此方法,以便它运行...但我希望调用该函数每次Session.get('activeDecision)
更改时都会自动更改,因此我不需要在每个事件上调用它,因为事件实际发生Session.set('activeDecision', value')
这是我的功能
this.showResults = ->
if Session.get("activeDecision")
$(document).mousemove Meteor.rotatePointer
$decisionId = Session.get("activeDecision")._id
decisionDiv = "#" + $decisionId
if decisionIsVoted($decisionId)
opcion_roja_total = Session.get("activeDecision").redTotal
opcion_azul_total = Session.get("activeDecision").blueTotal
if (parseInt(opcion_roja_total) + parseInt(opcion_azul_total)) > 0
percentage1 = Math.round((parseInt(opcion_azul_total) * 100) / (parseInt(opcion_roja_total) + parseInt(opcion_azul_total)))
percentage2 = Math.round((parseInt(opcion_roja_total) * 100) / (parseInt(opcion_roja_total) + parseInt(opcion_azul_total)))
else
percentage1 = 0
percentage2 = 0
$(percentage: 0).animate
percentage: percentage1
,
duration: 1000
easing: "swing"
step: ->
$(decisionDiv + " .blue-choice .percentage span").html Math.ceil(@percentage) + "%"
$(percentage: 0).animate
percentage: percentage2
,
duration: 1000
easing: "swing"
step: ->
$(decisionDiv + " .red-choice .percentage span").html Math.ceil(@percentage) + "%"
$(decisionDiv + " .option").fadeOut "fast", ->
$(decisionDiv + " .result").fadeIn "slow"
else
$(decisionDiv + " .result").fadeOut "fast", ->
$(decisionDiv + " .option").fadeIn "slow"
如果我不手动调用此方法,即使Session更改也不会运行...我该如何处理?
答案 0 :(得分:2)
如果我正确理解了问题,您想使用template autorun。类似的东西:
Template.home.onRendered ->
@autorun showResults
每当其反应变量发生变化时, showResults
将被评估,并且当模板被销毁时,autorun
将被自动清除。