简体   繁体   English

骨干没有调用路由器功能吗?

[英]Backbone not calling the router function?

So I have the following backbone route: 所以我有以下主干路线:

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();
  }
});

it does come into this class because the initialize function is being called, when this route loads I see: dasddasd in the console, but I do no see asdasdasd 它确实进入了此类,因为调用了initialize函数,当加载此路由时,我看到:控制台中的dasddasd ,但看不到asdasdasd

The url is localhost:9000/#server_error 网址是localhost:9000/#server_error

Can some one explain what I am doing wrong? 有人可以解释我做错了吗? I am not sure what else I am suppose to provide for further information so please ask for any additional details. 我不确定我还想提供什么进一步的信息,因此请询问其他详细信息。

Additional 额外

The following is how the app gets registered: 以下是该应用程序的注册方式:

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();
    }
  }
}

This class extends: 此类扩展:

Nightbird.Routers.Core = Backbone.Router.extend({

  serverError: function(){
    Backbone.history.navigate("server_error", {trigger: true});
  }

});

Why such a simple abstraction, because this way any issue getting or posting or what have you can redirect you to a server error route. 为什么这么简单的抽象,因为通过这种方式,任何问题的获取或发布或您所拥有的都可以将您重定向到服务器错误路由。

Then in my index.html I do: 然后在我的index.html执行:

<!DOCTYPE html>
<html>
  <body>
    <div id="manage">
    </div>
  </body>
  <script src="js/compiled.js"></script>
  <script>
    Nightbird.initialize();
  </script>
</html>

I guess that the problem is the way that you're instantiating the Backbone Router Try to create the Backbone Router inheriting from Backbone.Router. 我想问题是您实例化Backbone Router的方式尝试创建从Backbone.Router继承的Backbone Router。

When you check if Backbone.History.started is true, it probably is not. 当您检查Backbone.History.started是否为true时,可能不是。 so it will go to else statement, and there at that moment Backbone.History.star() is undefined. 因此它将转到else语句,并且此时Backbone.History.star()是未定义的。 So it is never starting the Backbone.History 因此它永远不会启动骨干网。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM