骨干网.js路由器的严格参数匹配
Strict parameter matching in backbone.js router
我有一个路由器定义如下:
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});
因此,以下 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'
如何将my_resource/:id
路由限制为仅匹配数字参数,以便notFound
处理app.MyRouter.navigate('/my_resource/some_thing')
?
来自精细手册:
路线
router.route(route, name, [callback])
手动为路由器创建路由,
route
参数可以是路由字符串或正则表达式。来自路由或正则表达式的每个匹配捕获都将作为参数传递给回调。
所以你总是可以这样说:
this.route(/my_resource'/('d+)/, 'show')
在路由器的initialize
如果您需要对路由进行比 Backbone 的字符串模式更精细的控制。
相关文章:
- 要求未定义JS回调参数
- JS:检查URL中的参数,然后迭代一个参数为var的函数
- 从node.js向CasperJS传递参数数组
- angular.js:13424错误:[ng:areq]参数'fn'不是函数,getObject
- JS和three.JS,在浏览器中进行参数测试
- 如何将一个*参数*从JS传递到AS3
- 参数列表Three.js之后的未捕获语法错误:缺少)
- 建议将包含不同参数的内联JS转换为外部脚本
- 如何在mysql查询(NODE.js)中重复使用一个参数
- 在JS参数中放置引号
- 如何将Gruntfile.js中的参数传递到webdriverio规范
- 如何将.json文件作为参数导入.js文件并使用命令行启动它
- Moment.js不接受变量作为参数
- 是否可以使用Sinon.JS检查函数参数绑定是否正确
- Meteor.js只更新一个参数,而不是整个集合
- 使用应用程序状态js和视图在angular js url上传递两个参数
- 保存上一页参数 js, jsp.
- 将重复出现的代码“外包”到带有参数(js)的函数中
- 如何在我的节点中包含另一个参数.js Web 服务
- 如何获取函数参数'js中的文本值