javascript 有没有办法用 Backbone 捕获所有不匹配的路由?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11236338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to catch all non-matched routes with Backbone?
提问by evilcelery
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.
我想要一个“捕获所有”路由,当其他定义的路由都不匹配时运行。一种 404 NotFound 错误处理程序。
I've tried adding this, which works but prevents other routes from matching:
我试过添加这个,它可以工作,但会阻止其他路由匹配:
this.route(/(.*)/, 'notFound', this.notFound);
Anyone solved this problem before?
以前有人解决过这个问题吗?
回答by evilcelery
Answering here for completeness.
在这里回答完整性。
You can do this 2 ways. Define the regular expression using route()
, as in the question. However, due to this bugyou 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:
您可以通过 2 种方式执行此操作。使用 定义正则表达式route()
,如问题中所述。但是,由于此错误,您需要通过该route()
方法以相反的顺序定义所有路由(catchall 在顶部)。这会阻止您使用路由哈希,因此(我的)首选方法是:
routes: {
'users/search': 'searchUsers',
'users/:id': 'loadUser',
'*notFound': 'notFound'
}
The key '*notFound'
can actually be anything starting with *
. You just require characters after the *
to prevent a parsing error.
键'*notFound'
实际上可以是任何以*
. 您只需要在 之后的字符*
以防止解析错误。
回答by machineghost
There's another, arguably simpler/more elegant way to solve this. Backbone.History.start()
returns either true or false based on whether it matched a route or not. So, if you just do:
还有另一种可以说更简单/更优雅的方法来解决这个问题。 Backbone.History.start()
根据是否与路由匹配,返回 true 或 false。所以,如果你只是这样做:
if (!Backbone.history.start()) router.navigate('404', {trigger:true});
instead of the usual:
而不是通常的:
Backbone.History.start();
it will have the same effect as the other answers.
它将与其他答案具有相同的效果。
回答by Pawel Dobierski
This very tiny plugin gets its job done: https://github.com/STRML/backbone.routeNotFound
这个非常小的插件完成了它的工作:https: //github.com/STRML/backbone.routeNotFound
It's the most elegant and robust way of solving this issue I've found so far, however please keep in mind that by using it, you are messing with Backbone's internals.
这是迄今为止我发现的解决此问题的最优雅、最可靠的方法,但是请记住,使用它会弄乱 Backbone 的内部结构。
回答by Dan
Without an example of your current routing code I would presume ensuring your catch all route is the last route should work for you
如果没有您当前路由代码的示例,我会假设确保您的所有路线是最后一条路线应该适合您
回答by McGarnagle
Just add it as the last route in the list. That way it will only be matched as the fallback option.
只需将其添加为列表中的最后一条路线即可。这样它只会作为后备选项匹配。
回答by winthers
You can hack it in to backbone like this:
您可以像这样将其 hack 到主干上:
var _loadUrl = Backbone.history.loadUrl;
Backbone.history.loadUrl = function (fragment) {
var result = _loadUrl.apply(Backbone.history, fragment);
// the loadUrl returns false if no route was found.
if(!result){
// call 404 route on router if it exists.
var handler = Backbone.history.handlers.filter( o => o.route.test("404") )
if(handler.length) {
Backbone.history.navigate("/404")
}
}
}
Then in your router you can do like this:
然后在您的路由器中,您可以这样做:
var Router = Backbone.Router.extend({
routes: {
"404": "pageNotFound"
},
pageNotFound: function () {
alert("trigger pageNotFound route")
}
});
回答by Myd
I think this should not pass JavaScript to solve, should be the language to solve, for example php. On the server side processing of words, as long as you have the condition you can response head with error number 404
我觉得这个应该不是通过JavaScript来解决的,应该是语言来解决的,比如php。服务器端对word的处理,只要你有条件就可以响应head错误号404