骨干网 - 如何根据一些其他的属性而不是ID从集合中找到一个模式? [英] Backbone - How to find a model from a collection according to some other attribute but not ID?

查看:96
本文介绍了骨干网 - 如何根据一些其他的属性而不是ID从集合中找到一个模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的骨干,并有一个简单的问题必须回答。

I am new to Backbone, and have a simple question need to be answer.

所以,我有几个对象的模型:

So, 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 属性来实现这一目标。但我认为应该有一些更简单的方法来做到这一点。可能有人提供给我的code?

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. Could some one provide me the code?

感谢您

推荐答案

骨干集合支持underscorejs 找到方法,因此使用,应该工作。

Backbone collections support the underscorejs find method, so using that should work.

things.find(function(model) { return model.get('name') === 'Lee'; });

这篇关于骨干网 - 如何根据一些其他的属性而不是ID从集合中找到一个模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆