Javascript CompositeView,其中子项可以来自多个集合
简而言之,这就是我需要的 我需要显示一个下拉/组合框,其中的孩子可以来自许多集合。所有这些集合的模型都有一个文本属性,我将在下拉列表中显示该属性 我想知道是否有可能从许多集合中创建下拉列表 即:Javascript CompositeView,其中子项可以来自多个集合,javascript,backbone.js,typescript,marionette,Javascript,Backbone.js,Typescript,Marionette,简而言之,这就是我需要的 我需要显示一个下拉/组合框,其中的孩子可以来自许多集合。所有这些集合的模型都有一个文本属性,我将在下拉列表中显示该属性 我想知道是否有可能从许多集合中创建下拉列表 即: 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:
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>
A
A.
A.
Q
R
s
因此,如果我选择A,我希望能够得到A的带有真菌属性的模型,如果我选择R,我希望得到带有NumberOfBugs属性的R的模型
其思想是,它们是两个具有公共属性的集合,但在后端对应于不同的模型
在木偶/脊梁中有什么方法可以做到这一点吗
如何创建具有多个数据源/集合的下拉列表
合并到一个集合将不起作用,因为同步/获取操作将无法正常工作。如果这些集合在应用程序中始终同时工作,您可以从一开始将它们设置为单个集合,然后使用子集合,例如用于自定义行为和同步/获取:添加到子集合的模型也将添加到常规集合
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
希望对您有所帮助FilteredCollection的add方法被列入黑名单?@Milo-您所说的“黑名单”是什么意思?@Milo抱歉,如果没有
filterBy
函数,我的代码就不完整了,现在应该更有意义了。请注意,sulge.add
和bugs.add
只是general.add
的代理,因此可以不加区别地使用