Backbone.js 路由器路由不区分大小写
有谁知道如何使主干路由不区分大小写?
IE http://localhost/Products http://localhost/products
都会触发产品路由
routes: {
"products": "products"
},
更新
基于 mu 的答案太短了,这里是完整的路由器。感谢您的帮助
MainRouter = Backbone.Router.extend({
routes: {
"": "home",
"products": "products"
},
initialize: function () {
Backbone.history.start({ pushState: true });
},
home: function () {
// just show the products!!
this.products();
},
products: function () {
// wire up the view.
var ps = new ProductsView({ el: '#products', collection: myCollection });
ps.render();
// fetch the collection!!
myCollection.fetch();
},
_routeToRegExp: function (route) {
route = route.replace(this.escapeRegExp, "\\$&")
.replace(this.namedParam, "([^\/]*)")
.replace(this.splatParam, "(.*?)");
return new RegExp('^' + route + '$', 'i'); // Just add the 'i'
}
});
Does anyone know how to make the Backbone routes case insensitive?
I.E.
http://localhost/Products
http://localhost/products
both trigger the products route
routes: {
"products": "products"
},
Update
Based on mu is too short's answer, here is the full Router. Thanks for your help
MainRouter = Backbone.Router.extend({
routes: {
"": "home",
"products": "products"
},
initialize: function () {
Backbone.history.start({ pushState: true });
},
home: function () {
// just show the products!!
this.products();
},
products: function () {
// wire up the view.
var ps = new ProductsView({ el: '#products', collection: myCollection });
ps.render();
// fetch the collection!!
myCollection.fetch();
},
_routeToRegExp: function (route) {
route = route.replace(this.escapeRegExp, "\\amp;")
.replace(this.namedParam, "([^\/]*)")
.replace(this.splatParam, "(.*?)");
return new RegExp('^' + route + '
, 'i'); // Just add the 'i'
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Backbone.Router
的optamd3
版本似乎没有escapeRegExp
、namedParam
和splatParam
常量。此外,完全替换某个功能将来更有可能出现故障。this
中的我通过调用重写的函数并使用它的 RegExp 解决了这个问题:
The
optamd3
version ofBackbone.Router
does not seem to have theescapeRegExp
,namedParam
, andsplatParam
constants inthis
. In addition, entirely replacing a function is more likely to break in the future.I solved it by just calling the overridden function, and using its
RegExp
:您可以使用
Backbone.Router.route()
,它将接受字符串或 RegExp 对象形式的路由:或者您可以在
Backbone.Router
中替换此私有方法,同时子类化< /em> Backbone.Router.extend(),谢谢 Crescent Fresh)::
routes
对象使用Backbone.Router.route()
添加到路由表中,并使用_routeToRegExp
将它们转换为正则表达式。以下是
); }_routeToRegExp
方法的演示:http://jsfiddle.net/ambigously/ MDSC5/(通过 这个(您还必须复制/扩展
escapeRegExp
namedParam
和splatParam
正则表达式):
routes
对象使用Backbone.Router.route()
添加到路由表中,并使用_routeToRegExp
将它们转换为正则表达式。以下是
_routeToRegExp
方法的演示:http://jsfiddle.net/ambigously/ MDSC5/You could bind all your routes manually with
Backbone.Router.route()
, that will accept the route as a string or a RegExp object:Or you could replace this private method in
Backbone.Router
while subclassing (viaBackbone.Router.extend()
, thank you Crescent Fresh):The routes in the
routes
object are added to the routing table usingBackbone.Router.route()
and that converts them to regexes using_routeToRegExp
.Here's a demo of the
); }_routeToRegExp
approach: http://jsfiddle.net/ambiguous/MDSC5/with this one (you'll have to copy/expand the
escapeRegExp
namedParam
, andsplatParam
regexes too):The routes in the
routes
object are added to the routing table usingBackbone.Router.route()
and that converts them to regexes using_routeToRegExp
.Here's a demo of the
_routeToRegExp
approach: http://jsfiddle.net/ambiguous/MDSC5/