backbone.marionette.subrouter icon indicating copy to clipboard operation
backbone.marionette.subrouter copied to clipboard

route cleanup on subrouter destruct

Open mojoaxel opened this issue 8 years ago • 1 comments

I have multiple modules in a project where every module has its own subrouter. I want to be able to load/unload modules with their subrouter. At the moment routes only get added but not removed. Even worse: If I instantiate a subrouter, delete it and instantiate it again I end up with the same route-handler multiple times in Backbone.history.routes.

We should add a cleanup function or something to remove routes from the history if a subrouter gets destroyed! I'll look into it.

mojoaxel avatar Dec 01 '16 14:12 mojoaxel

I just added this function to the SubRouter:

cleanUp: function() {
  _.each(this.appRoutes, function(key, route) {
    Backbone.history.handlers = _.reject(Backbone.history.handlers, function(handler) {
      return String(handler.route).localeCompare(Backbone.Router.prototype._routeToRegExp(route)) === 0;
    });
  }, this);
}

This feels realy dirty! Maybe somebody got a better idea how to solve this?

mojoaxel avatar Dec 01 '16 17:12 mojoaxel