如何使用backbonecollections筛选模型
How to use backbone collections to filter models
我已经实现了一个具有多个模型和视图的应用程序,但是 collections 理解起来有点麻烦。到目前为止,我已经在不使用 collections 的情况下实现了我的目标,现在我需要根据属性操作一组模型。我很确定我现在需要 collections。
我有以下结构(比实际实现简单得多):
app.Connector=Backbone.Model.extend({
line: //a d3 line object
source: //a d3 group
target: //a d3 group
// and some functions
});
app.Set=Backbone.Collections.extend({
model:app.Connector;
url:"/set" //what is the purpose of url?
});
var set=new app.Set();
//multiple connectors are initialized
假设我有一个 d3 object obj
。如何获得 list/array 个连接器 obj
作为 target
?
var filtered = set.filter(d=>d.get('target') == obj)
我觉得 Backbone get
函数过于冗长,所以我想在过滤前将集合转换为 json。
var filtered = _.filter(set.toJSON(),d=>d.target == obj)
我已经实现了一个具有多个模型和视图的应用程序,但是 collections 理解起来有点麻烦。到目前为止,我已经在不使用 collections 的情况下实现了我的目标,现在我需要根据属性操作一组模型。我很确定我现在需要 collections。
我有以下结构(比实际实现简单得多):
app.Connector=Backbone.Model.extend({
line: //a d3 line object
source: //a d3 group
target: //a d3 group
// and some functions
});
app.Set=Backbone.Collections.extend({
model:app.Connector;
url:"/set" //what is the purpose of url?
});
var set=new app.Set();
//multiple connectors are initialized
假设我有一个 d3 object obj
。如何获得 list/array 个连接器 obj
作为 target
?
var filtered = set.filter(d=>d.get('target') == obj)
我觉得 Backbone get
函数过于冗长,所以我想在过滤前将集合转换为 json。
var filtered = _.filter(set.toJSON(),d=>d.target == obj)