如果没有获取模型,如何防止 Backbone.Marionette 渲染视图? [英] How to prevent Backbone.Marionette from rendering a view if it's model hasn't been fetched?

查看:18
本文介绍了如果没有获取模型,如何防止 Backbone.Marionette 渲染视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的backbone.Marionette 应用程序中,我有一个模型,它需要一个Id 属性来构造它的url.因此,我通过向模型传递 Id 来创建模型,将其添加到视图中,然后获取模型:

In my backbone.Marionette application I have a Model that requires an Id attribute to construct it's url. I therefore create the model by passing it an Id, add it to a view and then fetch the model:

   model = new Model({_id:id})               
   view = new View({model:model})                               
   app.content.show(view)                                                    
   model.fetch()

我希望视图仅在获取模型后才开始渲染,但 Marionette 会立即渲染模型,导致我的模板渲染失败,因为预期的属性不存在.有什么解决方法吗?

I would expect the view to only start rendering once the model has been fetched, but Marionette renders the model immediately causing my template rendering to fail as the expected attributes don't exist. Any workarounds?

我正在尝试做一些类似于这里接受的答案:将 Backbone 模型绑定到 Marionette ItemView - 阻塞 .fetch()?

I'm trying to do something similar to the accepted answer here: Binding a Backbone Model to a Marionette ItemView - blocking .fetch()?

虽然这适用于主干,如答案中所述,Marionette 会自动呈现视图.

But while that works with backbone, as stated in the answer, Marionette automatically renders the view.

另见:Backbone Marionette 在获取完成前显示

推荐答案

如果您真的想在获取模型之前阻止渲染,您应该像这样重新排列您的调用:

If you truly want to prevent rendering until the model has been fetched you should reorder your calls like this:

model = new Model({_id:id});
view = new View({model:model});
model.fetch({success: function () {
  app.content.show(view);
});

或者,您应该考虑立即渲染并利用 Marionette 的空白状态支持.

Alternatively, you should think about rendering immediately and taking advantage of Marionette's blank state support.

这篇关于如果没有获取模型,如何防止 Backbone.Marionette 渲染视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆