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?
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 are able to control the instantiation of your router, you can do the following:
var myRouter = new MyRouter({ routes: function(){
return;
}});
You can use hack-based solution (it uses non-API methods and may stop working with new versions of Backbone.js)
var router = new(Backbone.Router.extend({
routes: {
"authentication": "authentication",
"contacts": "contacts",
"*notFound": "notFound"
},
/**
* @param {string} routeName
*/
disableRoute: function(routeName) {
var index, handler, handlers = Backbone.history.handlers;
delete this.routes[routeName];
for (var i = 0, len = handlers.length; i < len; i++) {
handler = handlers[i];
if (handler.route.toString() === router._routeToRegExp(routeName).toString()) {
handlers.splice(index, 1);
break;
}
}
},
contacts: function() {
alert('route `contacts`');
},
authentication: function() {
alert('route `authentication`');
},
notFound: function() {
alert('route `notFound`');
router.navigate('404');
}
}));
Backbone.history.start({
silent: true
});
$(function() {
$('#remove').on('click', function() {
router.disableRoute('authentication');
router.navigate('404');
});
$('#goto_auth').on('click', function() {
router.navigate('authentication', {
trigger: true
});
});
$('#goto_contacts').on('click', function() {
router.navigate('contacts', {
trigger: true
});
});
});
button {
display: block;
margin: 10px;
}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"></script>
</head>
<body>
<button id="goto_auth">goto authentication route</button>
<button id="goto_contacts">goto contacts route</button>
<hr>
<button id="remove">remove authentication route</button>
</body>
</html>
© 2022 - 2024 — McMap. All rights reserved.