updateCurrent: function(fragment, options) {
var router = this,
hash = $.url(fragment).attr('fragment');
if (normalizeURL.call(this)) {
options = options || {};
// When fragment is undefined, Backbone internally gets a fragment and decode it
// In this case, we are also decoding our currentFragment so we can have a fair
// comparison between them
if (!fragment) {
currentFragment = decodeURI(currentFragment);
}
fragment = Backbone.history.getFragment(fragment);
if (fragment !== currentFragment) {
var handler = this.getHandlerByFragment(fragment);
if (handler) {
currentFragment = fragment;
// Clone so internal handler cannot be altered directly
router.currentHandler = _.clone(handler);
if (router._popstate.direction === 'back') {
var removedRoute = routeHistory.pop();
router.trigger('routeHistory:remove', removedRoute, router._popstate);
} else {
if(options.replace) {
routeHistory.pop();
}
var route = {
path: getHandlerPathByFragment(handler, fragment),
regexp: getHandlerRegExpByFragment(handler, fragment)
};
var url = window.location.origin + '/' + encodeURI(fragment);
if (!_.isEmpty(hash)) {
url += '#' + hash;
}
// Attach a reference to a jQuery purl() object for the route
route.$url = $.url(url);
// Get the Backbone route params and URL paramaters for easy reuse throughout the app
route.params = getParams.call(this, route, fragment);
var addedRoute = _.defaults(route, routeDefaults);
routeHistory.push(addedRoute);
if(!options.replace) {
router.trigger('routeHistory:add', addedRoute);
}
}
if (routeHistory.length > 0) {
// update the reference to the current route (clone routeHistory to keep it private)
router.currentRoute = _.clone(_.last(routeHistory));
// Reset popstate direction indicator for the next route
router._popstate.direction = '';
}
else if (router._popstate.direction === 'back') {
// We removed the last item from history; force page refresh
window.location.reload();
}
return true;
}
return false;
}
return true;
}
}