在您调用之前,Backbone 不会在您的路由器中访问初始路由Backbone.history.start
,因此您可以延迟它,直到您完成必要的设置。我通常 start
在我的应用程序的主路由器上定义一个方法。看起来像:
var AppRouter = Backbone.Router.extend({
start: function() {
//init code here
something.fetch({success: function() {
//only call history start after the necessary initial setup is done
Backbone.history.start();
}});
}
});
然后使用该方法启动应用程序:
window.app = new AppRouter();
window.app.start();
请记住,没有什么限制您仅使用Backbone
. 如果你的启动代码很重,它可能不属于路由器。在这种情况下,您应该定义一个辅助函数来封装启动逻辑,并将路由器完全排除在外:
//startup.js
function startup(onComplete) {
//do initialization stuff...
onComplete();
});
//main.js
startup(function() {
Backbone.history.start();
});