2015-05-02 58 views
0

我有一个流星包内的工作,在一个js文件的全局函数通过我添加api.addFiles流星包:Session.set并不

showLayer = function(id) { 
    var layers = Session.get('layers'); 
    layers[id] = true; 
    Session.set('layers', layers); 

    // just for testing - check if it worked 
    var tmpLayers = Session.get('layers'); 
    alert('layers.id:' + layers[id] + ' tmpLayers.id:' + tmpLayers[id]); 
    window.setTimeout(function() { 
     var tmpLayers = Session.get('layers'); 
     alert('layers.id:' + layers[id] + ' tmpLayers.id:' + tmpLayers[id]); 
    }, 1000); 
}; 

注意,setTimeout我只使用给Session有些时候如果应该有一些优化来延缓这种变化。结果是layers[id]被设置为应该的,但tmpLayers[id]不是,延迟测试,并在set之后。

不管我检查通过setTimeout或右延迟到位内部功能:该Session.set线似乎并没有做任何东西无论如何!

请注意,这不是没有发生反应的问题 - 或者至少我不会看到如何。

+0

您是否在'package.js'中添加了'api.use('session');'? –

+0

不幸的是 - 我正在使用'session'和'meteor-platform',它应该包括所有其他的。我现在还添加了上面的非延迟测试代码来澄清。 –

回答

0

简短的解决方案:使用{}而不是[]。

我发现了一个可能的原因,通过这个堆栈溢出问题:Javascript arrays and Meteor session

但是:在那里提供的解决方案不是为我解决了这个问题。 解决这个问题的方法是使[]和{}。不需要克隆,它开箱即用。我认为这个问题可能是一个错误,但我不能轻易重现它。