变量

时间:2016-12-08 20:54:29

标签: javascript meteor

我的一个类中有一个跟踪器,这个类从另一个类中导入一个变量,该类执行一些计算并返回一个对象数组。然后我尝试用这些数据填充表格。当我为db提取数据时,它通常可以找到,因为跟踪器依赖于它,但是当我使用数组时它似乎没有依赖于变量而只运行跟踪器一次又返回半空数组。

正如您在本示例中所看到的,我有badDebtsNameSpace是从另一个类导入的数组变量。但它始终返回undefined。

Tracker.autorun(function() { // Tracker function for reactivity

        if(typeof badDebtsNameSpace.globalDebCalc != "undefined"){
            cashInData[0] = badDebtsNameSpace.globalDebCalc['fromOpeningDeb'];
            cashInTable.loadData(cashInData);

            cashInTable.render();
        }

        console.log(badDebtsNameSpace)



});

如果我从控制台日志中查看打印数据,一切似乎都没问题。关于为什么会发生这种情况的任何想法。

谢谢

更新

根据要求提供更多代码。以下是导出变量的一些输出类。

export var prodNameSpace = {};
export var overheadsNameSpace = {};
export var fixedAssetsName = {};
export var fundingNameSpace = {};


export var globalDebCalc;
export var badDebtsNameSpace;

export var productSummary;
export var assetSummary;
export var vatSummary;
export var overheadsSummary;
export var fundingSummary;

//Overheads Tracker TO Calc Data
    Tracker.autorun(function() { // Tracker function for reactivity
        var tempData = Session.get('data');
        var overs = Overhead.find({fileId: tempData._id}).fetch();
        var globalData = GlobalWhatIfData.find({fileId: tempData._id}).fetch();

         if(overs.length > 0 && globalData.length > 0){
            for(var i in overs){
                var percentageOf = PercentageOfData.find({parentId: overs[i]._id}).fetch();
                var vatRef = VatDataRef.find({parentId: overs[i]._id}).fetch();

                if(percentageOf.length > 0 && vatRef.length > 0){
                    var n = overs[i].description;
                    overheadsNameSpace[n] = new OverheadCalc(productSummary, percentageOf, vatRef, globalData);
                }

            }   
            overheadsSummary = new OverheadsCalc(overheadsNameSpace);
         }
    });

    //Bad Debt Tracker 
    Tracker.autorun(function() { // Tracker function for reactivity
        var tempData = Session.get('data');
        var bds = BadDebts.find({fileId: tempData._id}).fetch();
        var globaDeb = GlobalDebtors.find({fileId: tempData._id}).fetch();

        if(bds.length > 0 && globaDeb.length > 0){
            for(var i in bds){
                badDebtsNameSpace = new BadDebtsCalc(bds, productSummary, globaDeb);
            }
        }
    });

1 个答案:

答案 0 :(得分:0)

您可以使用我在评论中提到的反应式数组执行此操作,但如果您不想添加包,则可以使用tracker dependency这基本上会记录对其他一些计算的依赖性,以便for(var i in array)可以对此做出反应。您还shouldn't使用下面的export var prodNameSpace = {}; export var overheadsNameSpace = {}; export var fixedAssetsName = {}; export var fundingNameSpace = {}; export var globalDebCalc; export var badDebtsNameSpace = []; export var bdnsDep = new Tracker.Dependency; // register a dependency export var productSummary; export var assetSummary; export var vatSummary; export var overheadsSummary; export var fundingSummary; //Overheads Tracker TO Calc Data Tracker.autorun(function() { // Tracker function for reactivity var id = Session.get('data')._id; // small simplification var overs = Overhead.find({fileId: id}).fetch(); var globalData = GlobalWhatIfData.find({fileId: id}).fetch(); if(globalData.length){ // you don't need to check >0 as 0 will be *falsy* overs.forEach(o=>{ var percentageOf = PercentageOfData.find({parentId: o._id}).fetch(); var vatRef = VatDataRef.find({parentId: o._id}).fetch(); if(percentageOf.length && vatRef.length){ var n = o.description; overheadsNameSpace[n] = new OverheadCalc(productSummary, percentageOf, vatRef, globalData); } } overheadsSummary = new OverheadsCalc(overheadsNameSpace); } }); //Bad Debt Tracker Tracker.autorun(function() { // Tracker function for reactivity var id = Session.get('data')._id; var bds = BadDebts.find({fileId: id}).fetch(); var globaDeb = GlobalDebtors.find({fileId: id}).fetch(); if(bds.length && globaDeb.length){ bdnsDep.changed(); // tell the tracker to register the change for(var i = 0; i < bds.length; i++){ badDebtsNameSpace[i] = new BadDebtsCalc(bds, productSummary, globaDeb); } } }); Tracker.autorun(function() { // Tracker function for reactivity bdnsDep.depend(); // this is where we react to the change if( badDebtsNameSpace.globalDebCalc ){ // you don't have to check the type, just check to see if the key exists cashInData[0] = badDebtsNameSpace.globalDebCalc['fromOpeningDeb']; cashInTable.loadData(cashInData); cashInTable.render(); } }); 代码迭代数组:

machines=("dell" "pb")
ips=( "192.168.0.70" "192.168.0.60")
n=-1
for nom_machine in "${machines[@]}"
do
        n=$(( $n + 1 ))
        ip_machine=${ips[$n]}
        link=" $ip_machine $nom_machine"
        $(sudo /bin/bash -c  'echo -e $link  >> /etc/hosts')

done