最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Routing.navigate vs document.location.hash - Stack Overflow

programmeradmin0浏览0评论

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'
    }
});

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'
    }
});
Share Improve this question edited Jun 30, 2012 at 9:14 dda 6,2132 gold badges27 silver badges35 bronze badges asked Jun 30, 2012 at 8:24 Lorraine BernardLorraine Bernard 13.4k23 gold badges85 silver badges138 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

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

发布评论

评论列表(0)

  1. 暂无评论