我为基于事件的集合比较编写了一个库。基本上,我认为你可以像这样使用这个库来实现你正在寻找的结果(没有测试过这个特定的案例):
cocomp = new Backbone.CoComp({
comparator: function(obj) {
obj["1C"] === obj["2C"]
}
});
// Listen for "cocomp:in:1C" on the 2C collection. This will be
// triggered if there is a match based on the comparator above.
2C.on("cocomp:in:1C", function(value) {
2C.remove(value);
});
cocomp.set("1C", 1C)
cocomp.set("2C", 2C)
GitHub 存储库在这里:https ://github.com/davidbiehl/backbone.cocomp
让我知道这是否有帮助!这个库的额外好处是,如果 1C 或 2C 发生变化(添加、删除或重置),如果匹配,将触发事件,有效地保持 1C 和 2C 同步。