我有一个定义的模型和集合:筛选骨干集合根据属性值
var Box = Backbone.Model.extend({
defaults: {
x: 0,
y: 0,
w: 1,
h: 1,
color: "black"
}
});
var Boxes = Backbone.Collection.extend({
model: Box
});
当收集填充了款,我需要做的是有一个特定的彩色包装盒型号的新箱集属性包含完整的集合中,我做这种方式:
var sorted = boxes.groupBy(function(box) {
return box.get("color");
});
var red_boxes = _.first(_.values(_.pick(sorted, "red")));
var red_collection = new Boxes;
red_boxes.each(function(box){
red_collection.add(box);
});
console.log(red_collection);
这工作,但我觉得有点复杂,效率不高。有没有办法以更简单的方式做同样的事情?
这里是我描述的代码:http://jsfiddle.net/HB88W/1/
将在模型中,这变化CID? – janetsmith 2013-03-11 13:30:21
filterBy:函数(属性,值){ });函数(box){0} 返回新盒子(过滤); } – Josh 2013-07-07 15:27:20
为什么要返回'new Boxes()'。我会写 VAR盒= Backbone.Collection.extend({ 型号:箱, byColor:功能(彩色){ 回报this.filter(函数(盒){ 回报box.get( “颜色”)= == color; }); } }); – marcoslhc 2013-10-09 20:03:02