Routing.navigate vs document.location.hash

Question!

I have built a web app using backbone.marionette. When, from a Marionette.ItemView, I trigger the event document.location.hash:

 document.location.hash = '#tasks/' + this.model.get('id');

1.a) it changes the URL 1.b) it triggers the appRoutes

If I trigger the Routing.navigate from the same place:

router.navigate('#tasks/' + this.model.get('id'))

2.a) it changes the URL as expected 2.b) it does not trigger the appRoutes.

Any idea why 2.b happens? Where could the issue be?

Thanks.

var Router = Marionette.AppRouter.extend({
    appRoutes: {
        'tasks': 'tasks',
        'tasks/:id': 'taskDetail',
        '*defaults': 'tasks'
    }
});


Answers

You need to add {trigger: true}

router.navigate('#tasks/'   this.model.get('id'), {trigger: true})

Generally I extend the router and then add my own navigate that automatically adds that {trigger: true}. I understand why the developers did it like that, but it isn't the way I've ever used it :)

By : Stephen


This video can help you solving your question :)
By: admin