Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing.navigate vs document.location.hash

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'
    }
});
like image 590
Lorraine Bernard Avatar asked Jan 17 '23 06:01

Lorraine Bernard


1 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 :)

like image 143
Stephen Avatar answered Jan 18 '23 22:01

Stephen