Hash and backbone router
-
18-06-2021 - |
Question
How should I make this using Router?
var itemView = Marionette.ItemView.extend({
events: {
'click #loadPage': 'loadPage'
},
loadPage: function () {
document.location.hash = '#tasks/' + this.model.get('id');
}
});
Solution
As per documentation, router.navigate is simply proxying to Backbone.history, which is global so you should be able to use it without problems:
Backbone.history.navigate("#tasks/", { trigger: true })
the {trigger: true}
options as expected will trigger the hash change so that your router can react if it has that route registered.
OTHER TIPS
You must have "global" variable with a Router instance.
Then just call that variable in this way:
// yourRouter was defined in your main controller like this:
// var YourRouterClass = Backbone.Router.extend({
// stuff of your own Router
// });
// var yourRouter = new YourRouterClass(); <- this is your global router instance
yourRouter.navigate('tasks/'+this.model.get('id'), {trigger : true});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow