CompositeView 其中 children 可以来自多个集合

CompositeView where children can come from multiple collections

简而言之,这是我需要的。

我需要展示一个 dropdown/combobox,其中 children 可以来自许多集合。所有这些集合的模型都有一个文本属性,我将在下拉列表中显示它。

我想知道是否可以从多个集合中创建下拉列表。

即:

Collection one has:
Text: A, Fungus: 9
Text: B, Fungus: 7
Text: C, Fungus: 6

Collection 2 has:
Text: Q, NumberOfBugs: 8
Text: R, NumberOfBugs: 9
Text: S, NumberOfBugs: 7

我的下拉列表最终看起来像:

<option>A</option>
<option>A</option>
<option>A</option>
<option>Q</option>
<option>R</option>
<option>S</option>

因此,如果我 select A,我希望能够获得具有 Fungus 属性的 A 模型,如果我 select R 我想要 R 的具有 NumberOfBugs 属性的模型。

想法是它们是具有共同属性的 2 个集合,但在后端对应不同的模型。

在 Marionette/Backbone 中有什么方法可以做到这一点吗?

创建具有多个数据源/集合的下拉列表的方法?

合并到一个集合中将不起作用,因为同步/提取操作将无法正常工作。

如果这些集合在您的应用程序中始终协同工作,您可以从一开始就将它们作为一个集合,然后使用类似 backbone-filtered-collection 的子集合来实现自定义行为和同步/获取:添加到子集合的模型会也加到通用的。

var general = new Backbone.Collection
var fungus = new FilteredCollection(general)
var bugs = new FilteredCollection(general)

fungus.filterBy(function(model){
  return model.has('fungus')
})

bugs.filterBy(function(model){
  return model.has('numberOfBugs')
})

general.add([
    {text: 'A', fungus: 9},
    {text: 'B', fungus: 7},
    {text: 'C', fungus: 6},
    {text: 'Q', numberOfBugs: 8},
    {text: 'R', numberOfBugs: 9},
    {text: 'S', numberOfBugs: 7}
  ])



// general.length => 6
// fungus.length => 3
// bugs.length => 3

希望对您有所帮助