所以,我在Marionette中处理这个问题 - 我有3个嵌套的CompositeViews。视图中的每个集合都取决于它的父级id。所以我只能在获取父模型后才能获取集合。我的问题是,如何在声明CompositeView之后填充集合。让我给你看一些代码:
Env.Comp2 = Marionette.CompositeView.extend({
...,
childView: Env.Comp3,
initialize: function () {
var _this = this;
$.when(getEntities('secondCollection')).done(function (collection) {
_this.collection = collection;
/**
* Here is a problem. Is there a better way to fill the
* collection from inside? Or is there a way to parse
* the collection from it's parent CompositeView?
**/
}).then(function () {
_this.render();
});
}
});
Env.Comp = Marionette.CompositeView.extend({
...,
childView: Env.Comp2
});
Env.Index = Marionette.LayoutView.extend({
...,
regions: { reg1: '#id' }
onShow: function () {
var _this = this;
$.when(getEntities('firstCollection')).done(function (collection) {
_this.getRegion('reg1').show(new Env.Comp({
collection: collection
}));
});
}
});
我确实简化了我的代码,但基本上它包含一个示例结构。