Backbone.js 如何设计MarionetteJS ItemView以正确显示加载消息?
首先,我是一个木偶 在获取ItemView时,我无法使其显示加载消息或抖动。当从主干网历史记录中的深层链接显示此ItemView时(即,ItemView是自用户直接链接到应用程序后显示的应用程序的第一页),这一问题尤其严重。我想指出页面正在加载(获取),最好是使用一个简单的视图,然后使用获取的模型显示真正的模板化视图 我已经看到了其他的答案,比如Marionette.async(已经被弃用)和在ItemView.initalize()期间更改模板 有人(Derrick?)有什么建议或最佳实践吗 更新: 我使用collection.get(id)从集合中获取模型,而不是直接使用model.fetch() 考虑到这一点,真正的问题是应该在哪里实施:Backbone.js 如何设计MarionetteJS ItemView以正确显示加载消息?,backbone.js,marionette,backbone-views,Backbone.js,Marionette,Backbone Views,首先,我是一个木偶 在获取ItemView时,我无法使其显示加载消息或抖动。当从主干网历史记录中的深层链接显示此ItemView时(即,ItemView是自用户直接链接到应用程序后显示的应用程序的第一页),这一问题尤其严重。我想指出页面正在加载(获取),最好是使用一个简单的视图,然后使用获取的模型显示真正的模板化视图 我已经看到了其他的答案,比如Marionette.async(已经被弃用)和在ItemView.initalize()期间更改模板 有人(Derrick?)有什么建议或最佳实践吗
app.ModelLayout = Backbone.Marionette.Layout.extend({
constructor: function(){
var args = Array.prototype.slice.apply(arguments);
Backbone.Marionette.Layout.prototype.constructor.apply(this, args);
// we want to know when the collection is loaded or changed significantly
this.listenTo(this.collection, "reset sync", this.resetHandler);
},
resetHandler: function () {
// whenever the collection is reset/sync'ed, we try to render the selected model
if (this.collection.length) {
// when there is no model, check to see if the collection is loaded and then try to get the
// specified id to render into this view
this.model = this.collection.get(this.id);
}
this.render();
},
getTemplate: function(){
// getTemplate will be called during render() processing.
// return a template based on state of collection, and state of model
if (this.model){
// normal case: we have a valid model, return the normal template
return this.template;
} else if (this.collection && this.collection.isSyncing) {
// collection is still syncing, tell the user that it is Loading
return this.loadingView;
} else {
// we're not syncing and we don't have a model, therefore, not found
return this.emptyView;
}
}
});
下面是如何使用它:
// display a single model on a page
app.Access.Layout.CardLayout = app.ModelLayout.extend({
regions: {
detailsRegion:"#detailsRegion",
eventsRegion:"#eventsRegion"
},
template:"CardLayout", // this is the normal template with a loaded model
loadingView:"LoadingView", // this is a template to show while loading the collection
emptyView:"PageNotFoundView", // this is a template to show when the model is not found
onRender : function() {
this.detailsRegion.show( blah );
this.eventsRegion.show( blah );
}
});
谢谢 用于项目视图
我认为可以在初始化函数中添加微调器,我非常喜欢spin.js,因为它非常容易使用,而且可以在Itemview的onRender函数中隐藏微调器
对于CollectionView
在CollectionView中,您可以这样处理它
看看德里克发布的解决方案。。
用于项目视图
我认为可以在初始化函数中添加微调器,我非常喜欢spin.js,因为它非常容易使用,而且可以在Itemview的onRender函数中隐藏微调器
对于CollectionView
在CollectionView中,您可以这样处理它
看看德里克发布的解决方案。。
我建议使用jQuery延迟:
ItemView
没有emptyView
属性,因此它无论如何都不适用于您的案例
更新:
根据您更新的问题,您应该仍然能够通过动态指定要使用的模板来应用该概念:
然后,当/如果成功获取数据,则在视图中触发重新渲染器。我建议使用jQuery延迟:
ItemView
没有emptyView
属性,因此它无论如何都不适用于您的案例
更新:
根据您更新的问题,您应该仍然能够通过动态指定要使用的模板来应用该概念:
然后,当/如果成功获取数据,则在视图中触发重新渲染。在发布问题之前,我实际上查看了您的一篇博客文章。它没有直接回答我的问题,因为我使用get(id)从集合中获取模型。我将更新我的问题以反映这一点。在发布问题之前,我实际上看了你的一篇博客文章。它没有直接回答我的问题,因为我使用get(id)从集合中获取模型。我将更新我的问题以反映这一点。感谢微调器函数的推荐。显示微调器不是问题的核心,而是知道何时显示什么视图以及在何处交换视图。感谢微调器函数的引用。显示微调器不是问题的核心,而是知道何时显示什么视图以及在何处交换视图。