为什么在给出第一个结果后仍然会调用此方法?

时间:2016-04-11 23:58:06

标签: javascript html5 twitter-bootstrap meteor

我正在开发一个MeteorJS应用程序,我正在动态添加一个数据目标属性来激活模态,其中三个添加正常并产生正确的结果,但第一个目标反复调用占用CPU的方法和RAM,打破了我想要完成的事情。我发现的一个修复是添加首先调用该方法的null / hidden标记,但这并不能解决重复调用占用CPU和RAM的问题。

HTML:

{{#each getCategories}}
    <div class="row hidden-sm hidden-md hidden-lg visible-xs-block">
      <div class="col-xs-10">
        <h2>{{this.category}}</h2>
      </div>
      <div class="col-xs-2">
        <!--
          Must have this null element for some odd reason
          the first call to getUniqueID continuous runs and breaks
          this functionality so this a quick fix
        -->
        <!-- <null style="display:none;">{{getUniqueID}}{{getUniqueID}}</null> -->
        <span data-toggle="modal" data-target="#submit_{{getUniqueID}}"><i class="fa fa-plus-circle fa-3x" aria-hidden="true"></i></span>
        {{>cardSumbitModal category=this.category id=getUniqueID}}
      </div>
    </div>
      {{#each cards this.category}}
      <div class="row hidden-sm hidden-md hidden-lg visible-xs-block">
        <div class="col-xs-12">
          {{>card id=_id color=../color}}
        </div>
      </div>
      {{/each}}
    {{/each}}

JS:

getUniqueID: function(){
        var id = Session.get("roomNumber");
        var count = Session.get("getUniqueID_CallCount");

        id = id + count;
        if(!Session.get("pairSet")){
            Session.set("pairSet", true);
        }else{
            count++;
            Session.set("pairSet", false);
        }
        Session.set("getUniqueID_CallCount",count);
        return id;
    }

1 个答案:

答案 0 :(得分:2)

Because helpers are designed to be reactive they can run much more often than you expect. You should setup the getCategories helper to map an array and include the unique id as a an extra column so it doesn't need to be computed with every document/row.