如何通过其ID从Backbone.js集合中获取模型? [英] How do I get a model from a Backbone.js collection by its id?
问题描述
在我的应用程序中,我对数据所做的一切都基于主键,因为数据存储在数据库中。我想从基于此键的集合中获取模型。
In my app, everything I do with data is based on the primary key as the data is stored in the database. I would like to grab a model from a collection based on this key.
使用Collection.at()需要数组索引,Collection.getByCid()需要客户端ID骨干随机生成。
Using Collection.at() requires the array index, Collection.getByCid() requires the client ID that backbone randomly generates.
从具有给定id值的集合中获取我想要的模型的最佳方法是什么?我认为我能做的最糟糕的事情就是迭代每个项目.get('id'),然后返回那个项目。
What is the best way to grab the model I want from the collection with the given id value? I figure the worst I could do would be to iterate over each item, .get('id'), and return that one.
推荐答案
看看get方法,它可能会有所帮助:)
Take a look at the get method, it may be of some help :)
http://backbonejs.org/#Collection-get
get collection.get(id)
从集合中获取模型,由id,cid或传入模型指定。
get collection.get(id)
Get a model from a collection, specified by an id, a cid, or by passing in a model.
这篇关于如何通过其ID从Backbone.js集合中获取模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!