サーバーにまだ保存されていないモデルについて、Collection.get(id) を使用して、cid によって Backbone.js コレクション内のモデルを検索できますか?
ドキュメントから、.get は id または cid のいずれかでモデルを見つける必要があるようです。ただし、collection.get(cid)
モデルは見つかりませんが、これは見つかりませんcollection.find(function(model) {return model.cid===cid; })
。おそらく私は何か基本的なことを見落としています。
var Element = Backbone.Model.extend({});
var Elements = Backbone.Collection.extend({ model: Element });
var elements = new Elements(), el, cids = [];
for (var i=0; i<4; i++) {
el = new Element({name: "element"+i})
elements.add(el);
cids.push(el.cid);
}
console.log(cids);
el1 = elements.get(cids[0]);
console.log(el1); // undefined
el1a = elements.find(function(model) { return model.cid === cids[0]; });
console.log(el1a); // success