这是我收藏的样本集:
{
"name": "Bob",
"class": "3",
"marks": {
"maths": 70,
"science": 85
}
},
{
"name": "Ron",
"class": "3",
"marks": {
"maths": 80,
"science": 90
}
}
使用 Backbone 的 findWhere,我可以根据这样的查询获得模型:
Coln.findWhere({"name": "Ron"});
但是如何根据数学标记查询模型?下面的代码似乎不起作用:
Coln.findWhere({"marks.maths": 80});
最佳答案
收藏有各种Underscore methods混入其中。特别是有 find
它允许您指定谓词函数,以便您可以执行以下操作:
Coln.find(function(m) { return m.get('marks').maths === 80 })
如果要查找所有匹配的模型,请使用
filter
.
关于backbone.js - Backbone - findWhere : How to find a model by querying for a nested object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25027783/