javascript - session 更改时函数中的 meteor react

标签 javascript coffeescript meteor

我有这个函数,当事件发生时手动调用它,例如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 发生变化,它也不会运行...我该如何处理?

最佳答案

如果我正确理解这个问题,你想使用 template autorun 。像这样的东西:

Template.home.onRendered ->
  @autorun showResults

showResults 将在其响应变量每次更改时进行评估,并且 autorun 将在模板被销毁时自动清理。

关于javascript - session 更改时函数中的 meteor react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23848411/

相关文章:

javascript - axios获取请求-img src链接响应格式-/正在转换为空格

javascript - 这个 jquery/javascript 循环的更短替代方案?

javascript - 使用 Coffeescript 读取本地文件

javascript - 在 CoffeeScript 中声明 Backbone 集合

selenium - 无法在 Protractor 和 CoffeeScript 中访问 WebDriverJS 等待 'until'

javascript - 如何仅在 Mongo 集合中的特定字段包含特定值时运行函数?

javascript - 使用 vue multiselect 过滤表格

javascript - 使用 Angularjs 为下拉菜单设置不同的文本/值

arrays - 在数组上执行 findOne() 时,Meteor 和 Mongo DB 会提供不同的结果

meteor - meteor 中输入文本返回事件