简体   繁体   English

使用Backbone直接调用url

[英]Directly calling url with Backbone

If I navigate to a view by clicking on a link such as 127.0.0.1/#/project/1 , the correct view gets displayed. 如果我通过单击诸如127.0.0.1/#/project/1的链接导航到视图, 127.0.0.1/#/project/1显示正确的视图。 However, if I call this url directly in the browser (or hit refresh), the view won't be displayed. 但是,如果我直接在浏览器中调用此URL(或单击刷新),则不会显示该视图。 What could be the reason for this behaviour? 这种行为可能是什么原因?

The way I set up the Router is as follows: 我设置路由器的方法如下:

var AppRouter = Backbone.Router.extend({
      routes: { },
      initialize:function () { }
});
var app = new AppRouter();

and then in every module (I'm using require.js), a route and handler will be added 然后在每个模块中(我正在使用require.js),将添加一个路由和处理程序

app.route("project/:id", "showProject");

Could it be that the routes aren't registered yet and thus the callbacks won't be called? 可能是路由尚未注册,因此不会调用回调?

Make sure that you are calling Backbone.history.start() after all of your routers are loaded/instantiated and routes defined: http://backbonejs.org/#History-start 确保在加载/实例化所有路由器并定义路由之后,正在调用Backbone.history.start() : //backbonejs.org/#History-start

Alternatively, you could stop the history with Backbone.history.stop() , and start it again. 或者,您可以使用Backbone.history.stop()停止历史记录,然后重新启动。 Then the added route(s) will be picked up. 然后将选择添加的路线。

BTW, you can test if the history is currently started with the boolean Backbone.History.started (note the capital 'H' is necessary). 顺便说一句,您可以测试历史记录当前是否以布尔值Backbone.History.started开始(注意,大写Backbone.History.started “ H”是必需的)。

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

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