我的路由器是这样的:
define(function(require) {
var _ = require('underscore'),
Backbone = require('backbone'),
homeStageView,
homeSidebarView,
yxyStageView;
return Backbone.Router.extend({
routes: {
'/web/:route' : 'viewLoader'
},
initialize: function() {
//this._bindRoutes();
$('.link').click(function(e) {
e.preventDefault();
Backbone.history.navigate($(this).attr('href'), true);
});
},
viewLoader: function(route) {
switch(route) {
case 'home':
this.homeHandler();
break;
case 'yxy':
this.yxyHandler();
break;
}
},
// navigation handlers
homeHandler: function() {
if ( !homeStageView ) {
require(['views/home-stage-view'], function(HomeStageView) {
homeStageView = new HomeStageView();
homeStageView.render();
});
}
else {
homeStageView.render();
}
this.renderHomeSidebarView();
},
yxyHandler: function() {
if ( !yxyStageView ) {
require(['views/yxy-stage-view'], function(YxyStageView) {
yxyStageView = new YxyStageView();
yxyStageView.render();
});
}
else {
yxyStageView.render();
}
this.renderHomeSidebarView();
},
});
});
在我的主视图中,我像这样初始化路由器:
appRouter = new AppRouter();
if ( history && history.pushState ) {
Backbone.history.start({pushState: true});
console.log('has pushState');
}
else {
Backbone.history.start();
console.log('no pushState');
}
这适用于加载我的所有视图,但当用户单击后退按钮时它不起作用。
地址栏中的 url 可能会相应地出现,但仅此而已。显然,我错过了一些东西。
有人可以帮忙吗?