【发布时间】:2012-09-05 17:15:01
【问题描述】:
更新
我为我的项目创建了一个变通方案。我将一个空模板设置为此视图默认模板(实际上包含加载消息和 throbber,以防进一步延迟)并设置模型更改事件以在准备好时用 groupTemplate 替换视图 $el html。这在我的开发、测试和生产机器上实现了无缝过渡,但是,如果在检索模型数据时生产速度变慢,它实际上会显示加载消息。
define([
'underscore',
'backbone',
'marionette',
'models/group',
'text!templates/shared/empty.html', // include empty template
'text!templates/groups/group.html'
], function(_, Backbone, Marionette, groupModel, emptyTemplate, groupTemplate){
var GroupView = Backbone.Marionette.ItemView.extend({
initialize: function() {
var view = this;
this.model = new groupModel({id:this.options.groupid});
// listen to the model change and display the real template when fired.
this.model.on('change', function(){
view.$el.html(_.template(groupTemplate, view.model.attributes));
});
this.model.fetch();
},
template: emptyTemplate // display empty template be default
});
return GroupView;
});
我有一个使用嵌套列布局(在本例中为三列)的单一布局。嵌套布局使用相应的视图调用,它需要这些视图并将它们显示在它自己的区域中。 (如果有更好的方法,请告诉我)。
我的问题是 col2 的 GroupView(嵌套三列布局)正在使用 ItemView,并且正在使用没有数据可启动的模型。该模型使用 url 而不是从集合中检索数据,因此必须使用 fetch 来检索数据。 ItemView 正在尝试使用没有数据的模型来呈现它的模板,并且无法找到模型字段。给我以下错误。
Uncaught ReferenceError: groupname is not defined
有没有办法延迟渲染,或者 ItemView 有没有像 colletionsView 这样的未记录的 emptyView?如果有更好的处理方法,请告诉我。
路由器控制器
group: function(groupid) {
// require and setup our three column layout
require([
'views/layouts/three-column', 'views/shared/left-user-menu',
'views/groups/group', 'views/shared/location'
], function(threeColumnLayout, leftView, groupView, locationView) {
App.wrapper.currentView.main.show(new threeColumnLayout({
views: {
col1: new leftView(),
col2: new groupView({groupid: groupid}),
col3: new locationView()}
}));
}
嵌套布局(三栏)
define([
'underscore',
'backbone',
'marionette',
'text!templates/layouts/three-column.html'
], function(_, Backbone, Marionette, threeColumnTemplate) {
var Layout = Backbone.Marionette.Layout.extend({
initialize: function(options) {
var layout = this;
this.on('render', function(options){
layout.col1.show(options.options.views.col1);
layout.col2.show(options.options.views.col2);
layout.col3.show(options.options.views.col3);
});
},
tagName: 'div',
template: threeColumnTemplate,
regions: {
col1: '#col1',
col2: '#col2',
col3: '#col3'
}
});
return Layout;
});
分组项目视图
define([
'underscore',
'backbone',
'marionette',
'models/group',
'text!templates/groups/group.html'
], function(_, Backbone, Marionette, groupModel, groupTemplate){
var GroupView = Backbone.Marionette.ItemView.extend({
initialize: function() {
this.model = new groupModel({id:this.options.groupid});
this.model.fetch();
},
template: groupTemplate
});
return GroupView;
});
【问题讨论】:
-
看看 Marionette.Async。它旨在使
show处理异步请求。 -
@tkone 我尝试使用异步模块,但我的项目的其余部分停止工作。它似乎是一种全有或全无的类型。此外,我用一个可以与 RequireJS 和文本模板一起使用的模板缓存来覆盖……所以这也很奇怪。我将继续研究异步模块,看看我是否使用不正确。