触发同位置路线

发布于 2024-12-21 21:44:14 字数 95 浏览 0 评论 0原文

有没有办法在backbone.js中触发相同的位置路由,例如当位置为/My/App/#/About并且用户再次单击具有相同路由的锚点以刷新页面时内容。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

坏尐絯℡ 2024-12-28 21:44:14
Backbone.history.loadUrl(Backbone.history.fragment);

您的具体问题的解决方案并未真正记录下来,似乎建议的最佳实践是仅调用您想要触发的路由中声明的函数:/

Ref:https://github.com/documentcloud/backbone/issues/1214

到目前为止,其他答案是正确的,但他们忘记提及,如果哈希导航到的是与当前哈希相同,不会触发任何内容。

Backbone.history.loadUrl(Backbone.history.fragment);

The solution for your specific problem isn't really documented and it seems the suggested best practice is to just call the function that is declared in the route you want to fire :/

Ref: https://github.com/documentcloud/backbone/issues/1214

So far the other answers are correct, but they forgot to mention that if the hash navigated to is the same as the current hash, nothing will fire.

Router.navigate('about', true);

这样您就可以手动触发路线。

Router.navigate('about', true);

That way you can trigger a route manually.

揪着可爱 2024-12-28 21:44:14

假设您不仅想对“关于”页面执行此操作,请使用:

route = Backbone.history.fragment;
yourRouter.navigate(route, true);

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

route = Backbone.history.fragment;
yourRouter.navigate(route, true);
上课铃就是安魂曲 2024-12-28 21:44:14

由于 Backbone 路由器会查找要加载的 URL 片段中的更改,因此对我来说有效的是首先在不触发的情况下导航到“/”,然后再导航到要使用触发器命中的 URL(片段)。如果您愿意,您可以很容易地将其构建到默认路由器行为中。

例如

App.Routers.appRouter.navigate '/', {trigger: false}
App.Routers.appRouter.navigate myPath, {trigger: true}

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}
苏璃陌 2024-12-28 21:44:14

使用 Backbone 0.9.2,您传入一个选项对象并将“trigger”设置为“true”。

router.navigate('some/route', { trigger : true });

参考:http://documentcloud.github.com/backbone/#Router-navigate

Using Backbone 0.9.2 you pass in an options object and set 'trigger' to 'true'.

router.navigate('some/route', { trigger : true });

Reference: http://documentcloud.github.com/backbone/#Router-navigate

飘过的浮云 2024-12-28 21:44:14

另一种选择:您可以添加一个具有随机值的参数,因此每次调用的哈希值都会不同,即使您调用同一页面。

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.

不可一世的女人 2024-12-28 21:44:14

感谢@eddywashere 的建议,真的很棒。我在我的中添加了一些编辑,检查您是否位于最后一个(最新的)网址,如果是,则加载该历史记录。很简单,认为它可以帮助遇到同样问题的人。

$("[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);
  }
});

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);
  }
});
旧情勿念 2024-12-28 21:44:14
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.
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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文