So I need to remove a router in backbone.js
to prevent it's routes from occuring. I've tried myRouter.off()
and myRouter.remove()
without any luck.
What can I do instead?
There's no officially supported way to do this (that I know of). If you want to disable any router, you can use Backbone.history.stop();
, which is undocumented, but shows up in the source code with this comment:
// Disable Backbone.history, perhaps temporarily. Not useful in a real app,
// but possibly useful for unit testing Routers.
Otherwise, you'd have to code some passthrough condition in your router's route handlers if the state of the router is "disabled" or something like that. Or iterate on the undocumented Backbone.history.handlers (the internal array containing the .route - as a regexp - and .callback) and remove the routes related to this specific router.
Obviously, being undocumented and all, this is subject to change in future releases of Backbone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With