Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unbind a router in backbone.js

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?

like image 696
Industrial Avatar asked Feb 13 '12 13:02

Industrial


1 Answers

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.

like image 118
mna Avatar answered Dec 08 '22 00:12

mna