我有两种布局。第一个布局负责渲染一个子布局(由于我的界面相当复杂,很快就会有很多带有视图的子布局)。
这是第一个布局:
App.module('Layouts', function(Layouts, App, Backbone, Marionette, $, _) {
Layouts.Editor = Backbone.Marionette.Layout.extend({
tagName: 'div',
className: 'content container-fluid',
regions: {
stageContainer: '#stage-container',
},
initialize: function() {
this.template = Backbone.Marionette.TemplateCache.get('editor');
},
render: function() {
var _this = this;
this.model = new App.Models.App();
this.model.url = GLOBAL.api.url + '/clients/' + GLOBAL.cid + '/sweepstakes/' + GLOBAL.aid;
this.model.fetch({ success: function() {
// show template
$(_this.el).html(_this.template());
// show region content
_this.showStageContainer();
} });
},
showStageContainer: function() {
var content = new App.Layouts.EditorStageContainer({
model: this.model
});
this.stageContainer.show(content);
return content;
}
});
});
这渲染得很好。但是,当我尝试在showStageContainer
函数中渲染我的子布局时,会调用渲染,但实际上并未渲染我的内容。值得注意的是,我在获取模型后调用了 showStageContainer,因为我需要将模型传递给布局和子视图。舞台容器布局如下所示:
App.module('Layouts', function(Layouts, App, Backbone, Marionette, $, _) {
Layouts.EditorStageContainer = Backbone.Marionette.Layout.extend({
el: '#stage-container',
tagName: 'div',
regions: {
nav: '#nav',
progress: '#progress',
stage: '#stage'
},
initialize: function() {
this.template = Backbone.Marionette.TemplateCache.get('editor_stage_container');
},
render: function() {
console.log('render');
$(this.el).html(this.template());
},
onShow: function() {
// TODO: figure out why the template has to be rendered on show rather
// than in the render. (render is never being called as onRender is not called)
$(this.el).html(this.template());
// show region content
this.showNav();
},
showNav: function() {
var content = new App.Views.EditorStageNav();
this.nav.show(content);
}
});
});
如您所见,我不得不在onShow
函数内部手动渲染模板。通常,这应该在render
函数内自动发生。
最有趣的是,如果我在渲染函数中包裹了一个 setTimeout $(this.el).html(this.template());
,一切都很好......这让我相信某些事情没有按顺序执行。例子:
var _this = this;
render: function() {
setTimeout(function() {
$(_this.el).html(_this.template());
}, 1);
}
有什么想法吗?在这里不知所措,真的可以使用一些帮助。谢谢!