获取模型后渲染木偶区域 [英] Render a Marionette region after model has been fetched
问题描述
我想使用 Derick Bailey 在此 线程 在获取模型后呈现视图.我会在这里报告他的解决方案:
I would like to use the approach described by Derick Bailey in "A Generic Problem Solution" in this thread to render a view after a model is fetched. I will report his solution here:
MyView = Backbone.View.extend({
initialize: function(){
this.model.on("sync", this.render, this);
},
render: function(){ ... }
});
myModel = new MyModel({id: someId});
new MyView({
model: myModel
});
myModel.fetch();
我的情况略有不同:我的视图位于区域布局内.如果我调用 Marionette.Region.show() 它可以工作,但视图会呈现两次.调用 Marionette.Region.attachView() 会调用一次视图渲染函数,但页面中不显示内容.
I have a slightly different situation: my view is inside a region layout. If I call the Marionette.Region.show() it works but the view is rendered twice. Calling Marionette.Region.attachView() the view render's function is called once but the content is not displayed in the page.
有什么想法吗?
推荐答案
可以等到模型同步后再渲染视图
You can wait until the model is sync'd before rendering the view
var myView = new MyView({
model: myModel
});
myModel.on("sync", function(){
myRegion.show(myView);
});
myModel.fetch();
这篇关于获取模型后渲染木偶区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!