如何根据ID之外的某些属性从集合中查找模型? [英] How to find a model from a collection according to some attribute other than the ID?
本文介绍了如何根据ID之外的某些属性从集合中查找模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含多个对象的模型:
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);
现在我想根据朋友的名字得到一个模型。我知道我可以添加一个 ID
属性来实现这一目的。但我认为应该有一些更简单的方法来做到这一点。
and now I want to get a model according to the name of the Friend. I know that I can add an ID
attribute to achieve this. But I think there should have some more simple way to do this.
推荐答案
Backbone集合支持underscorejs 找到
方法,所以使用它应该有效。
Backbone collections support the underscorejs find
method, so using that should work.
things.find(function(model) { return model.get('name') === 'Lee'; });
这篇关于如何根据ID之外的某些属性从集合中查找模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文