所以我有以下主干路由:
Nightbird.Routers.Errors = Nightbird.Routers.Core.extend({
routes: {
'server_error': 'serverError',
},
initialize: function(){
console.log('dasddasd');
},
serverError: function() {
console.log('asdasdasd');
var serverErrorView = new Nightbird.Views.ServerError();
serverErrorView.render();
}
});
它确实进入此类,因为正在调用 initialize
函数,当此路由加载时,我在控制台中看到:dasddasd
,但我没有看到asdasdasd
网址为localhost:9000/#server_error
有人可以解释一下我做错了什么吗?我不确定我还应该提供什么来获取更多信息,因此请询问任何其他详细信息。
附加
以下是应用程序的注册方式:
window.Nightbird = {
Models: {},
Collections: {},
Views: {},
Routers: {},
blogId: 0,
initialize: function() {
if (window.Development === undefined && window.Production === undefined) {
throw 'Production class (Production.config.js) cannot be missing. App Cannot load.';
}
if (window.Development !== undefined) {
this.blogId = window.Development.BLOG_ID;
} else {
this.blogId = window.Production.BLOG_ID;
}
new Nightbird.Routers.Posts();
new Nightbird.Routers.Errors();
if (!Backbone.History.started) {
Backbone.history.start();
} else {
Backbone.history.stop();
Backbone.history.start();
}
}
}
该类扩展:
Nightbird.Routers.Core = Backbone.Router.extend({
serverError: function(){
Backbone.history.navigate("server_error", {trigger: true});
}
});
为什么要这么简单的抽象,因为这样任何获取或发布问题或您拥有的问题都可以将您重定向到服务器错误路由。
然后在我的 index.html
中我这样做:
<!DOCTYPE html>
<html>
<body>
<div id="manage">
</div>
</body>
<script src="js/compiled.js"></script>
<script>
Nightbird.initialize();
</script>
</html>
最佳答案
我猜问题是你实例化 Backbone 路由器的方式 尝试创建继承 Backbone.Router 的 Backbone Router。
当您检查 Backbone.History.started 是否为 true 时,它可能不是。所以它将转到 else 语句,此时 Backbone.History.star() 未定义。所以它永远不会启动 Backbone.History
希望有帮助。
关于javascript - 主干网没有调用路由器功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25571947/