我有代码试图跟踪 Backbone js 应用程序中的当前片段。
$(function(){
Backbone.history.start({pushState: true});
console.log("fragment " + Backbone.history.fragment);
// Beef is a global that holds my router (created elsewhere)
Beef.router.bind("all", function(route) {
console.log("fragment from event handler " + Backbone.history.fragment);
});
});
此代码按预期打印“片段 xxx”,但当我在应用程序中导航时始终打印“未定义事件处理程序的片段”。
如果我首先将 Backbone.History 复制到本地 var,它会起作用:
$(function(){
Backbone.history.start({pushState: true});
console.log("fragment " + Backbone.history.fragment);
var hist = Backbone.history;
Beef.router.bind("all", function(route) {
console.log("fragment from event handler " + hist.fragment);
});
});
有人可以解释这里发生了什么吗?