Javascript 主干网不调用路由器功能?
因此,我有以下主干路线:Javascript 主干网不调用路由器功能?,javascript,backbone.js,Javascript,Backbone.js,因此,我有以下主干路线: Nightbird.Routers.Errors = Nightbird.Routers.Core.extend({ routes: { 'server_error': 'serverError', }, initialize: function(){ console.log('dasddasd'); }, serverError: function() { console.log('asdasdasd'); v
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
函数,当此路由加载时,我在控制台中看到:dasdasd
,但我没有看到asdasdasd
url是localhost:9000/#server\u 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>
Nightbird.initialize();
我想问题在于您实例化主干路由器的方式
尝试创建从Backbone.Router继承的主干路由器
当您检查Backbone.History.started是否为真时,可能不是。所以它将转到else语句,此时Backbone.History.star()未定义。因此,它从来没有开始骨干。历史
希望能有所帮助。你有以下代码吗:
主干网。历史记录。开始()
?@SlawaEremkin我更新了帖子-是的,我更新了。我不明白的是为什么我不能定义自己的自定义核心路由器,从主干网扩展而来-也许这不是不能的情况,这更像是一个例子——为什么这个扩展会有问题?window.Nightbird是一个简单的散列。它不是从主干网视图、主干网模型或主干网路由器继承的……你是对的,但在Nightbird.Routers.Core
的情况下,它确实是从backbone.router
扩展的,所以继承链只扩展了一个。如果有帮助的话,我也可以向您展示这些代码:DSo,Nightbird.Routers.Core是从Bakcbone路由器扩展而来的。。好的,但是Nightbird.Routers.Errors不是。。。试着模仿夜鸟。路由器从主干路由器向我们延伸。