如何忽略backbone.history.start上的URL中的哈希



我有一个像mysite.com/en/bla-bla/bla/bla/page#someid这样的URL浏览器应将窗口滚动到某个页面位置,如果在哈希URL中找到某种窗口。

此外,我在页面上有骨干,如果用户打开页面,则在URL骨架中具有一些哈希值,如果没有定义的路由匹配当前URL ,则无法使用。骨干无法匹配#SomeID作为路径。

var result = Backbone.history.start();
        if (!result) {
            console.log("If no defined route matches the current URL");
        }

如何解决它?

您可以添加一个所有路线:

SPLAT零件*splat,可以匹配任意数量的URL组件。

var Router = Backbone.Router.extend({
    routes: {
        '*catchall': 'homeRoute',
        // any route defined further down takes precedence on the ones before.
        'real-route/:id': 'realRoute',
    },
    homeRoute: function() { /*...*/ },
    realRoute: function(id) { /*...*/ }
});

定义您想要骨干的任何路线,然后让任何未定义的路线被Splat捕获。

最新更新