简体   繁体   English

如何在Meteor中创建动态javascript语句?

[英]How to create a dynamic javascript statement in Meteor?

I want to set the value in a series of reactive vars like these below more efficiently: 我想更有效地在如下所示的一系列无功变量中设置值:

Template.instance().editType.set(false);
Template.instance().editZip.set(!template.editZip.get());
Template.instance().editHeadCount.set(false);
Template.instance().editDate.set(false);
Template.instance().editTime.set(false) ;
Template.instance().editDuration.set(false);

I have wrote the following function, but it doesn't work as I am getting an error (TypeError: Template.instance(...).eval is not a function): 我已经编写了以下函数,但是由于出现错误(TypeError:Template.instance(...)。eval不是函数)而无法正常工作:

function turnOffSelect(currentDiv) {
    var divArray = ['.editType', '.editZip', '.editHeadCount', '.editDate', '.editTime', '.editDuration' ]  
    for (i = 0; i < divArray.length; i++) {
        if (divArray[i] != currentDiv) {
            Template.instance().eval(divArray[i]).set(false);           
        }
    }
}

Please help 请帮忙

Can you not just index using bracket notation instead of resorting to eval? 您不仅可以使用方括号符号编制索引,还可以求助于eval吗?

function turnOffSelect(currentDiv) {
  ['editType', 'editZip', 'editHeadCount', 'editDate', 'editTime', 'editDuration'].forEach(
    el => { Template.instance()[el].set(false) }
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM