I have code that is attempting to keep track of the current fragment in a Backbone js application.
$(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);
});
});
This code prints 'fragment xxx ' as expected but always prints 'fragment from event handler undefined' when I navigate in the app.
If I copy Backbone.History to a local var first it works:
$(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);
});
});
Can someone explain whats going on here?