Javascript 如何根据除 ID 以外的某些属性从集合中查找模型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14420599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find a model from a collection according to some attribute other than the ID?
提问by Anar
I have a model with several object:
我有一个包含多个对象的模型:
//Model
Friend = Backbone.Model.extend({
//Create a model to hold friend attribute
name: null,
});
//objects
var f1 = new Friend({ name: "Lee" });
var f2 = new Friend({ name: "David"});
var f3 = new Friend({ name: "Lynn"});
and also, I will add these friends object to a collection:
而且,我会将这些朋友对象添加到集合中:
//Collection
Friends = Backbone.Collection.extend({
model: Friend,
});
Friends.add(f1);
Friends.add(f2);
Friends.add(f3);
and now I want to get a model according to the name of the Friend. I know that I can add an IDattribute to achieve this. But I think there should have some more simple way to do this.
现在我想根据朋友的名字得到一个模型。我知道我可以添加一个ID属性来实现这一点。但我认为应该有一些更简单的方法来做到这一点。
回答by mu is too short
For simple attribute based searches you can use Collection#where:
对于简单的基于属性的搜索,您可以使用Collection#where:
where
collection.where(attributes)Return an array of all the models in a collection that match the passed attributes. Useful for simple cases of
filter.
在哪里
collection.where(attributes)返回集合中与传递的属性匹配的所有模型的数组。对于简单的
filter.
So if friendsis your Friendsinstance, then:
因此,如果friends是您的Friends实例,则:
var lees = friends.where({ name: 'Lee' });
There's also Collection#findWhere(a later addition as noted in the comments):
还有Collection#findWhere(评论中指出的后来添加):
findWhere
collection.findWhere(attributes)Just like where, but directly returns only the first model in the collection that matches the passed attributes.
找到哪里
collection.findWhere(attributes)就像where一样,但直接只返回集合中与传递的属性匹配的第一个模型。
so if you're only after one then you can say things like:
所以如果你只追求一个,那么你可以这样说:
var lee = friends.findWhere({ name: 'Lee' });
回答by Jani Hartikainen
Backbone collections support the underscorejs findmethod, so using that should work.
Backbone 集合支持 underscorejsfind方法,所以使用它应该可以工作。
things.find(function(model) { return model.get('name') === 'Lee'; });
回答by coderC
The simplest way is to use "idAttribute" option of Backbone Model to let Backbone know the that you want to use "name" as your Model Id.
最简单的方法是使用 Backbone Model 的“idAttribute”选项让 Backbone 知道你想使用“name”作为你的模型 ID。
Friend = Backbone.Model.extend({
//Create a model to hold friend attribute
name: null,
idAttribute: 'name'
});
Now you can directly use Collection.get() method to retrieve a friend using his name. This way Backbone does not loop through all of your Friend models in the Collection but can directly fetch a model based on its "name".
现在您可以直接使用 Collection.get() 方法来检索使用他的名字的朋友。这样 Backbone 不会遍历 Collection 中的所有 Friend 模型,而是可以根据模型的“名称”直接获取模型。
var lee = friends.get('Lee');
回答by Silver Ringvee
You can call findWhere()on Backbone collections, that will return exactly the model you are looking for.
您可以调用findWhere()Backbone 集合,它将准确返回您正在寻找的模型。
Example:
例子:
var lee = friends.findWhere({ name: 'Lee' });

