我使用的方法where
我收集主幹,像這樣一個骨幹模式:訪問從collection.where
var quote = app.Collections.quotes.where({Id: parseInt(id, 10)});
但是,要訪問的唯一結果/模型(因爲它是由ID,只有一個) - 我怎樣才能得到實際的模型,而不訴諸於此:
var onlyModel = quote[0]
?
有沒有更好的方法?
我使用的方法where
我收集主幹,像這樣一個骨幹模式:訪問從collection.where
var quote = app.Collections.quotes.where({Id: parseInt(id, 10)});
但是,要訪問的唯一結果/模型(因爲它是由ID,只有一個) - 我怎樣才能得到實際的模型,而不訴諸於此:
var onlyModel = quote[0]
?
有沒有更好的方法?
更好的方法是在集合上使用get
。 http://backbonejs.org/#Collection-get
var quote = app.Collection.quotes.get(parseInt(id, 10));
骨幹代理下劃線上的集合,並且將返回找到的第一個匹配功能顯着findWhere。
findWhere _.findWhere(列表屬性)
看起來通過列表,並返回匹配所有在屬性中列出的鍵 - 值對的第一個值。
您的查詢可以寫成
var quote = app.Collections.quotes.findWhere({Id: parseInt(id, 10)});
但在你的情況,如果你確實找給定ID的模型,你可以直接使用get方法
獲取collection.get(id)
從集合中獲取模型,由id,cid,或通過傳入模型。
var quote = app.Collection.quotes.get(id);