我无法通过 Backbone 使用导航栏导航到不同的路线。单击#logo
并#home
正确将我发送到index
. 然而,点击#demo
也将我发送到index
而不是demo
路线。为什么?
导航栏.js
var HeaderNavView = Backbone.View.extend({
tagName: 'div',
template: _.template( HeaderNavTemplate ),
events: {
'click #logo': 'goToIndex',
'click #home': 'goToIndex',
'click #demo': 'goToDemo'
},
render: function() {
var template = this.template();
this.$el.html(template);
return this;
},
goToIndex: function() {
console.log('go to index');
IndexRouter = new Backbone.Router();
var route = '';
IndexRouter.navigate(route, {trigger: true});
},
goToDemo: function() {
console.log('header nav to demo route')
demoRouter = new Backbone.Router();
var route = 'demo';
demoRouter.navigate(route, {trigger: true});
}
});
路由.js
Router = Backbone.Router.extend({
routes: {
'': 'index',
'demo': 'demo'
},
index: function() {
$('#videoIndex').append( IndexTemplate )
var headerNavView = new HeaderNavView;
$('#indexHeaderNav').append( headerNavView.render().el );
...additional nested views
},
demo: function() {
$('#demoDetails').append( DemoTemplate );
...additional nested views
}
});