justinhelmer
3/7/2017 - 6:57 PM

uc-new.js

updateCurrent: function(fragment, options) {
  var router = this;

  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);

          // There's a chance we hit back outside the bounds of route history (i.e. back after page refresh).
          // In this case, we must manually set up the route for execute
          if (routeHistory.length === 0) {
            router.setCurrentRoute(prepareRoute(handler, fragment));
          }
        } else {
          if (options.replace) {
            routeHistory.pop();
          }

          var addedRoute = prepareRoute(handler, fragment);

          routeHistory.push(addedRoute);

          if (!options.replace) {
            router.trigger('routeHistory:add', addedRoute);
          }
        }

        if (routeHistory.length > 0) {
          router.setCurrentRoute(_.last(routeHistory));
        }
        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;
  }
}