[英]Strict parameter matching in backbone.js router
I've got a router defined as such: 我有一个路由器定义如下:
var MyRouter = Backbone.Router.extend({
routes: {
// catch-all for undefined routes
"*notfound" : "notFound",
},
initialize: function(options) {
this.route("my_resource/clear_filters", 'clearFilters');
this.route("my_resource/:id", 'show');
},
show: function(id){
console.log('show', id);
},
clearFilters: function() {
console.log('clearFilters');
},
notFound: function() {
console.log('notFound');
},
});
var app = {};
app.MyRouter = new MyRouter();
Backbone.history.start({silent: true});
Thus the following URLs would map as: 因此,以下URL将映射为:
var opts = {trigger: true};
app.MyRouter.navigate('/foo', opts); // logged -> 'notFound'
app.MyRouter.navigate('/my_resource/123', opts); // logged -> 'show', '123'
app.MyRouter.navigate('/my_resource/clear_filters', opts); // logged -> 'clearFilters'
app.MyRouter.navigate('/my_resource/some_thing', opts); // logged -> 'show', 'some_thing'
How can I restrict the my_resource/:id
route to only match on numeric parameters so that app.MyRouter.navigate('/my_resource/some_thing')
is handled by notFound
? 如何限制
my_resource/:id
路由仅匹配数字参数,以便app.MyRouter.navigate('/my_resource/some_thing')
由notFound
处理?
From the fine manual : 从精细手册 :
route
router.route(route, name, [callback])
route
router.route(route, name, [callback])
Manually create a route for the router, The
route
argument may be a routing string or regular expression.手动为路由器创建路由,
route
参数可以是路由字符串或正则表达式。 Each matching capture from the route or regular expression will be passed as an argument to the callback.路由或正则表达式中的每个匹配捕获都将作为参数传递给回调。
So you can always say things like: 所以你总能说出这样的话:
this.route(/my_resource\/(\d+)/, 'show')
in your router's initialize
if you need finer grained control over the routes than Backbone's string patterns give you. 在你的路由器的
initialize
如果你需要对路由进行更细粒度的控制,而不是Backbone的字符串模式给你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.