Backbone.js:如何通过模型 ID 数组过滤对象集合?
我有一个充满模型的 Backbone.Collection
;假设该模型是Car
。这个集合是一个很棒的、很大的汽车
列表。我希望能够从列表中选择一些特定的汽车 ID,然后能够从该集合中获取那些选定的汽车对象。
我下面的代码块不起作用;我确信有一种方法可以使用 Backbone.js/Underscore.js 来做到这一点……我对 Backbone/Underscore 也很陌生。
CarList = Backbone.Collection.extend({
model: Car,
filterWithIds: function(ids) {
return this.filter(function(aCar) { return _.contains(ids, car.id); }
}
});
有什么指点吗?
I've got a Backbone.Collection
full of models; let's say that model is Car
. This collection is a great, big list of Cars
. I want to be able to have a few specific car IDs selected from a list, and then be able to get just those selected car objects out of this collection.
My code block below isn't working; I'm sure there's a way to do this with Backbone.js/Underscore.js… I'm pretty fresh to Backbone/Underscore, too.
CarList = Backbone.Collection.extend({
model: Car,
filterWithIds: function(ids) {
return this.filter(function(aCar) { return _.contains(ids, car.id); }
}
});
Any pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我想我已经明白了。它与我的原始代码块很接近,但更新后的
filterWithIds
函数位于此处。对于那些关注 CoffeeScript 的人(我就是),这里是 CoffeeScript 版本。
这是我的回答;有代码味道吗?
Okay, I think I've got it. It's close to my original code block, but the updated
filterWithIds
function is here.For those following along in CoffeeScript (I am), here's the CoffeeScript version.
It's my answer; any code smell?