Backbone.js 中调用的重复路由

发布于 2024-12-18 10:11:47 字数 833 浏览 0 评论 0原文

我想要一种如下所示的 RESTful URL 结构:

  • /accounts
  • /accounts/account/123

我已经这样设置了我的路由:

    MyRouter = Backbone.Router.extend({

        routes : {
            '/accounts':                         'accounts',
            '/accounts/account/:account':        'account',
        },
        accounts: function() {
            console.log('accounts CALLED');
        },
        account: function() { 
            console.log('account CALLED');
        },
    });

问题是当我转到 /accounts/account/ 时123accounts()account() 都被调用(如URL 与两者都匹配路线)。我尝试了诸如 /accounts$ 之类的路由,但看起来路由哈希不支持它。

有办法做到这一点吗?手动 router.route(route, name,callback) 可以代替吗(尽管我真的不想这样做)。

I want to have a kind of RESTful URL structure like below:

  • /accounts
  • /accounts/account/123

I've set up my routes as such:


    MyRouter = Backbone.Router.extend({

        routes : {
            '/accounts':                         'accounts',
            '/accounts/account/:account':        'account',
        },
        accounts: function() {
            console.log('accounts CALLED');
        },
        account: function() { 
            console.log('account CALLED');
        },
    });

The problem, is when I go to /accounts/account/123 , both accounts() and account() get called (as the URL matches both routes). I tried a route such as /accounts$, but it doesn't look like it's supported in the routes hash.

Is there a way to accomplish this? Would a manual router.route(route, name, callback) work instead (although I really prefer not to do that).

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

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

发布评论

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

评论(2

唐婉 2024-12-25 10:11:47

我通过另一个问题解决了这个问题。我没有意识到我必须严格使用router.navigate函数。

以编程方式使用路由器(而不是通过浏览器栏),那些重复的调用就会消失。我还看到使用 router.navigate 时仅调用一次预期函数。

我仍然需要找出如何捕捉回来&前进按钮来调用这些函数。感谢迄今为止的反馈。

I got this cleared up by another SO question. I didn't realise that I had to use the router.navigate function strictly.

Using the router programmatically (not via browser bar), those duplicate calls go away. I'm also seeing the expected functions called only once... when using router.navigate.

I still have to find out how to capture back & forward buttons to call those functions. Thanks for the feedback so far.

早乙女 2024-12-25 10:11:47

尝试使用 /accounts$ 代替。 $ 是字符串结尾的正则表达式。

Try /accounts$ instead. $ is the regex for the end of the string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文