我做了一个有趣的观察。当尝试更新存储在 Meteor 会话存储中的数组时,以下代码将不会传播更改:
var tags = Session.get("Tags");
tags.push("a");
Session.set("Tags", tags);
但是如果我改变第一行来使用Session.get("Tags").slice()
,取决于会话的所有内容都会相应更新。我想这是因为 Meteor 测试了一些引用的相等性,因此不会更新任何内容。
有没有更好的方法来管理存储在 Meteor 会话存储中的列表?
如果我现在尝试从集合中删除一个元素(使用array.remove()
from here http://ejohn.org/blog/javascript-array-remove/),结果是有点……的……我在 Meteor 模板事件中执行此操作,代码如下所示:
"click .taglist li" : function(e) {
var tags = Session.get("Tags").slice();
var index = cardTags.indexOf(this);
Meteor._debug(Session.get("Tags").slice().indexOf("a"));
Meteor._debug("Removing tag \"" + this + "\", index: " + index, ", typeof(this) = " + typeof(this).toString());
tags.remove(index);
Session.set("Tags", tags);
}
这输出:
1
Removing tag "a", index: -1, typeof(this) = string
所以不知何故,cardTags.indexOf(this);
声明似乎返回-1
几乎适用于任何情况。我想我正在做一些根本上错误的事情,因为我现在已经开始使用javascript了,但不知何故我无法弄清楚这里发生了什么。
为什么对 indexOf() 的这两次调用会有不同的行为?