Cha*_*les 7 backbone.js backbone-views backbone-routing
我一直在做关于backbone.js中的嵌套视图的一堆阅读,我理解了很多,但有一点让我感到困惑的是这......
如果我的应用程序有一个shell视图,其中包含在使用应用程序过程中不会改变的页面导航,页脚等子视图,我是否需要为每个路径渲染shell或者我是否做某种检查视图以查看它是否已存在?
如果有人在应用程序中前进之前没有点击"主页"路线,那对我来说似乎是这样.
我在google搜索中没有找到任何有用的信息,所以任何建议都表示赞赏.
谢谢!
jev*_*lio 16
由于您的"shell"或"layout"视图永远不会更改,因此您应该在应用程序启动时(触发任何路径之前)渲染它,并将更多视图渲染到布局视图中.
假设您的布局看起来像这样:
<body>
<section id="layout">
<section id="header"></section>
<section id="container"></section>
<section id="footer"></section>
</section>
</body>
Run Code Online (Sandbox Code Playgroud)
您的布局视图可能如下所示:
var LayoutView = Backbone.View.extend({
el:"#layout",
render: function() {
this.$("#header").html((this.header = new HeaderView()).render().el);
this.$("#footer").html((this.footer = new FooterView()).render().el);
return this;
},
renderChild: function(view) {
if(this.child)
this.child.remove();
this.$("#container").html((this.child = view).render().el);
}
});
Run Code Online (Sandbox Code Playgroud)
然后,您将在应用程序启动时设置布局:
var layout = new LayoutView().render();
var router = new AppRouter({layout:layout});
Backbone.history.start();
Run Code Online (Sandbox Code Playgroud)
在您的路由器代码中:
var AppRouter = Backbone.Router.extend({
initialize: function(options) {
this.layout = options.layout;
},
home: function() {
this.layout.renderChild(new HomeView());
},
other: function() {
this.layout.renderChild(new OtherView());
}
});
Run Code Online (Sandbox Code Playgroud)
有很多方法可以为这只特殊的猫提供皮肤,但这是我通常处理它的方式.这为您提供了一个单点控制(renderChild)来渲染"顶级"视图,并确保在remove渲染新元素之前前一个元素是d.如果您需要更改视图的呈现方式,这也可能会派上用场.
| 归档时间: |
|
| 查看次数: |
1566 次 |
| 最近记录: |