Backbone.js 路由器路由不区分大小写

发布于 2024-12-09 17:40:41 字数 1125 浏览 1 评论 0原文

有谁知道如何使主干路由不区分大小写?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最终幸福 2024-12-16 17:40:41

Backbone.Routeroptamd3 版本似乎没有 escapeRegExpnamedParamsplatParam this 中的 常量。此外,完全替换某个功能将来更有可能出现故障。

我通过调用重写的函数并使用它的 RegExp 解决了这个问题:

var Router = Backbone.Router.extend({

  _routeToRegExp: function(route) {
    route = Backbone.Router.prototype._routeToRegExp.call(this, route);
    return new RegExp(route.source, 'i'); // Just add the 'i'
  },

  ...

});

The optamd3 version of Backbone.Router does not seem to have the escapeRegExp, namedParam, and splatParam constants in this. 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:

var Router = Backbone.Router.extend({

  _routeToRegExp: function(route) {
    route = Backbone.Router.prototype._routeToRegExp.call(this, route);
    return new RegExp(route.source, 'i'); // Just add the 'i'
  },

  ...

});
醉态萌生 2024-12-16 17:40:41

您可以使用 Backbone.Router.route(),它将接受字符串或 RegExp 对象形式的路由:

Backbone.Router.route(/products/i, ...

或者您可以在 Backbone.Router 中替换此私有方法,同时子类化< /em> Backbone.Router.extend(),谢谢 Crescent Fresh):

_routeToRegExp : function(route) {
  route = route.replace(escapeRegExp, "\\
amp;")
               .replace(namedParam, "([^\/]*)")
               .replace(splatParam, "(.*?)");
  return new RegExp('^' + route + '

(通过 这个(您还必须复制/扩展 escapeRegExp namedParamsplatParam 正则表达式)

_routeToRegExp : function(route) {
  route = route.replace(/[-[\]{}()+?.,\\^$|#\s]/g, "\\
amp;")
               .replace(/:\w+/g, "([^\/]*)")
               .replace(/\*\w+/g, "(.*?)");
  return new RegExp('^' + route + '

routes 对象使用 Backbone.Router.route() 添加到路由表中,并使用 _routeToRegExp 将它们转换为正则表达式。

以下是 _routeToRegExp 方法的演示:http://jsfiddle.net/ambigously/ MDSC5/

); }

(通过 这个(您还必须复制/扩展 escapeRegExp namedParamsplatParam 正则表达式)


routes 对象使用 Backbone.Router.route() 添加到路由表中,并使用 _routeToRegExp 将它们转换为正则表达式。

以下是 _routeToRegExp 方法的演示:http://jsfiddle.net/ambigously/ MDSC5/

, 'i'); // Just add the 'i' }

routes 对象使用 Backbone.Router.route() 添加到路由表中,并使用 _routeToRegExp 将它们转换为正则表达式。

以下是 _routeToRegExp 方法的演示:http://jsfiddle.net/ambigously/ MDSC5/

); }

(通过 这个(您还必须复制/扩展 escapeRegExp namedParamsplatParam 正则表达式)

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:

Backbone.Router.route(/products/i, ...

Or you could replace this private method in Backbone.Router while subclassing (via Backbone.Router.extend(), thank you Crescent Fresh):

_routeToRegExp : function(route) {
  route = route.replace(escapeRegExp, "\\
amp;")
               .replace(namedParam, "([^\/]*)")
               .replace(splatParam, "(.*?)");
  return new RegExp('^' + route + '

with this one (you'll have to copy/expand the escapeRegExp namedParam, and splatParam regexes too):

_routeToRegExp : function(route) {
  route = route.replace(/[-[\]{}()+?.,\\^$|#\s]/g, "\\
amp;")
               .replace(/:\w+/g, "([^\/]*)")
               .replace(/\*\w+/g, "(.*?)");
  return new RegExp('^' + route + '

The routes in the routes object are added to the routing table using Backbone.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, and splatParam regexes too):


The routes in the routes object are added to the routing table using Backbone.Router.route() and that converts them to regexes using _routeToRegExp.

Here's a demo of the _routeToRegExp approach: http://jsfiddle.net/ambiguous/MDSC5/

, 'i'); // Just add the 'i' }

The routes in the routes object are added to the routing table using Backbone.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, and splatParam regexes too):

The routes in the routes object are added to the routing table using Backbone.Router.route() and that converts them to regexes using _routeToRegExp.

Here's a demo of the _routeToRegExp approach: http://jsfiddle.net/ambiguous/MDSC5/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文