javascript 如何提取 Backbone 集合的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7587383/
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 pluck a Backbone collection's attribute
提问by Philip Schweiger
I want to create an array of specific attribute values from a Backbone collection.
我想从 Backbone 集合中创建一个包含特定属性值的数组。
var days = _.select(
this.collection.models,
function(model) {
return model.attributes.type === 'session';
}
);
days = _.pluck(days, 'attributes'),
days = _.pluck(days, 'date');
This works, but seems inefficient. Is there a way to accomplish the same thing without having to define days
three times?
这有效,但似乎效率低下。有没有办法不用定义days
三次就可以完成同样的事情?
回答by Derick Bailey
pluck is a convenience method that wraps map
, and map
is available directly on the collection, which should make this easier.
勇气是一个方便的方法,它包装map
,并且map
可直接在收集,这应该更容易些。
assuming you are trying to get the date
attribute out of your models, you can do this:
假设您试图date
从模型中获取属性,您可以这样做:
days = this.collection.map(function(model){
return model.get('date');
});
your select call is also available on the collection directly, as the filter
method.
您的 select 调用也可以直接在集合上使用,作为filter
方法。
days = this.collection.filter(function(model){
return model.attributes.type === 'session';
});
you can chain these two together, but it helps if you define methods separately:
您可以将这两个链接在一起,但如果您单独定义方法会有所帮助:
var sessionFilter = function(model){
return model.attributes.type === 'session';
};
var getDate = function(model){ return model.get('date'); }
days = this.collection.filter(sessionFilter).map(getDate);
this should return the results your looking for... or something close to this at least :)
这应该返回您正在寻找的结果......或至少与此接近的结果:)
回答by Philip Schweiger
I should have read the docs more carefully. In addition to the pluck
method in underscore, backbone collections also have a pluck
method.
我应该更仔细地阅读文档。除了pluck
下划线中的方法,骨干集合也有pluck
方法。
http://documentcloud.github.com/backbone/#Collection-pluck
http://documentcloud.github.com/backbone/#Collection-pluck
So, my solution would be
所以,我的解决方案是
//Assumme 'collection' is a backbone collection
collection.pluck('date');
I still need to figure out how to best combine with filtering - perhaps using ideas from @Derick's answer, but this answers the meat of my own question.
我仍然需要弄清楚如何最好地结合过滤 - 也许使用@Derick 的答案中的想法,但这回答了我自己的问题。
回答by Henrik Aasted S?rensen
I think this could work :
我认为这可以工作:
var days =
_( this.collection.where({ type : "session" }))
.chain()
.pluck("attributes")
.pluck("date")
.value()
Slightly more elegant, but still close to unreadable, in my opinion.
在我看来,稍微优雅一点,但仍然接近于不可读。
回答by Rain Diao
Have the same question, and figured out a better solution. I might have just taken advantage of new features in underscore, since it's 2013 now :)
有同样的问题,并想出了更好的解决方案。我可能刚刚在下划线中利用了新功能,因为现在是 2013 年:)
var days = _.invoke(this.collection.where({type : 'session'}), 'get', 'date');