有没有办法赶上与骨干网的所有非匹配的路由? [英] Is there a way to catch all non-matched routes with Backbone?

查看:87
本文介绍了有没有办法赶上与骨干网的所有非匹配的路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有,当没有其他定义路由的匹配运行的'所有'的路线。 A型404 NOTFOUND错误处理。

I want to have a 'catch all' route which runs when none of the other defined routes are matched. A type of 404 NotFound error handler.

我试着从匹配补充这一点,这工作,但prevents其他路线:

I've tried adding this, which works but prevents other routes from matching:

this.route(/(.*)/, 'notFound', this.notFound);

任何人之前解决这个问题?

Anyone solved this problem before?

推荐答案

在这里回答的完整性。

您可以这样做2种方式。定义使用路线正规前pression(),如问题。然而,由于这个bug 你需要定义通过路线的所有路由( )方法,并以相反的顺序(顶部包罗万象)。使用路由散,所以这prevents你(我)preferred方法是:

You can do this 2 ways. Define the regular expression using route(), as in the question. However, due to this bug you would need to define all your routes via the route() method, and in reverse order (catchall at top). This prevents you from using the routes hash, so (my) preferred method is:

routes: {
  'users/search': 'searchUsers',
  'users/:id': 'loadUser',

  '*notFound': 'notFound'
}

关键'* NOTFOUND 其实可以是任何与开始* 。你只需要字符后的 * 来prevent解析错误。

The key '*notFound' can actually be anything starting with *. You just require characters after the * to prevent a parsing error.

这篇关于有没有办法赶上与骨干网的所有非匹配的路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆