public
Created — forked from mataspetrikas/gist:811849

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
// Backbone.history with HTML5 support
(function() {
var loc = window.location,
pushSupport = !!(window.history && window.history.pushState),
hashStrip = /^#*/;
 
// add HTML5 support to Backbone.history, drop the old IE stuff
_.extend(Backbone.History.prototype, {
 
getFragment : function(l) {
l = l || window.location;
if (pushSupport){
return l.pathname;
} else {
return l.hash.replace(hashStrip, '') || "/";
}
},
 
start : function() {
if (pushSupport) {
// modern browsers
$(window).bind('popstate', this.checkUrl);
} else if ('onhashchange' in window) {
// older browsers without pushState support
if (loc.pathname === "/") {
$(window).bind('hashchange', this.checkUrl);
} else {
// automatically redirect browsers to the BB-readable path
var hashPath = "/#" + loc.pathname;
loc.replace(hashPath);
return;
}
}
return this.loadUrl();
},
saveLocation : function(fragment) {
//alert("new fragment: " + fragment)
fragment = (fragment || '').replace(hashStrip, '');
if (this.fragment == fragment)
return;
if (pushSupport) {
this.fragment = fragment;
history.pushState({
ts: new Date().getTime()
}, document.title, loc.protocol + "//" + loc.host + fragment);
} else {
window.location.hash = this.fragment = fragment;
}
// pass along fragment, as pushState seems to be async on Android
this.loadUrl(fragment);
},
loadUrl : function(fragment) {
fragment = this.fragment = (fragment || this.getFragment());
var matched = _.any(this.handlers, function(handler) {
if (handler.route.test(fragment)) {
handler.callback(fragment);
return true;
}
});
return matched;
}
 
});
})();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.