Trigger same location route

2019-02-04 09:32发布

Is there a way to trigger same location route in backbone.js, for example when location is /My/App/#/About and user clicks again on anchor with the same route in order to refresh the page content.

8条回答
我欲成王,谁敢阻挡
2楼-- · 2019-02-04 10:13
Router.navigate('about', true);

That way you can trigger a route manually.

查看更多
仙女界的扛把子
3楼-- · 2019-02-04 10:13

Assuming you want to do this for more than just the About page, use:

route = Backbone.history.fragment;
yourRouter.navigate(route, true);
查看更多
走好不送
4楼-- · 2019-02-04 10:14

Since the Backbone router looks for changes in the URL fragment you want to load, what worked for me is to navigate to '/' first without trigger, and then subsequently to the URL (fragment) you want to hit with the trigger. You could probably build this into the default router behavior pretty easily if you wanted.

e.g.

App.Routers.appRouter.navigate '/', {trigger: false}
App.Routers.appRouter.navigate myPath, {trigger: true}
查看更多
SAY GOODBYE
5楼-- · 2019-02-04 10:17

Thanks for the suggestion @eddywashere, truly awesome. I added a little edit to mine that checks if you're on the last (most currentl) url, and if so, loads that history. Pretty simple, thought it might help someone with the same issue.

$("[data-href]").on("click", function(e) {
  var href;
  e.preventDefault();
  href = $(e.currentTarget).data("href");
  if ((_.last(this.history)) === href) {
    return Backbone.history.loadUrl(_.last(this.history));
  } else {
    return this.router.navigate(href, true);
  }
});
查看更多
别忘想泡老子
6楼-- · 2019-02-04 10:19

Another option : you can add a parameter with a random value, so hash will be different for each call, even if you call the same page.

查看更多
兄弟一词,经得起流年.
7楼-- · 2019-02-04 10:22
router.navigate(route, {trigger: false}); // Changes URL but it will not trigger...Ex: In place of route -- 'about/1' or 'about' etc

Backbone.history.loadUrl(Backbone.history.fragment); // Changed route will be triggered here.
查看更多
登录 后发表回答