【发布时间】:2016-03-02 02:07:30
【问题描述】:
我正在尝试在 Backbone 上进行路由,但此时它对我来说非常神秘。这就是我所拥有的
routes: {
'' : 'home',
'home' : 'home',
'departments' : 'departments',
'employees' : 'employees',
'requests' : 'requests'
},
home: function(){
new app.HomeView();
},
这是在我的路由器内部,显然我调用了 Backbone.history.start()。在我看来,我有定义渲染函数并从初始化方法调用它的模式。
el: '#containerList',
template: Handlebars.compile( $('#home-template').html() ),
initialize: function(){
this.render();
},
render: function(){
this.$el.html( this.template());
return this;
},
HTML:
<section id='main' class='container'>
<section id='containerList'></section>
</section>
我有以下问题:
- 在返回按钮上,视图被附加。我需要重新加载页面来解决这个问题。
- 每当在浏览器搜索栏上写入路线时,我都需要重新加载两次以显示视图。它的行为非常不稳定,有时它会在没有集合的情况下呈现视图。
我已经阅读了很多策略,要在 CloseView 上使用事件,在离开视图时使用 this.remove。显然这对我没有任何作用。我几乎看不到主干上的路由器文档。
【问题讨论】:
-
由于您要替换整个 html,例如
this.$el.html( this.template());,因此不太可能添加某些内容...您可以创建一个 minimal reproducible example..?