【问题标题】:Routing in Backbone.js / Marionette.js - no hashtags, route list and sub-routersBackbone.js / Marionette.js 中的路由 - 没有主题标签、路由列表和子路由器
【发布时间】:2013-08-16 13:33:06
【问题描述】:

我有三个关于 Backbone.js / Marionette.js 中路由的问题:

  • 1) 如何获取应用程序的路由器已注册的所有路由的列表?

例如,对于 Express.js(在 Node.js 中),它将是 app.routes

我正在尝试对 Backbone.js / Marionette.js 执行相同的操作,但找不到执行此操作的任何属性或方法。

  • 2) 我想清理我的 URL 并删除它们前面的主题标签“#”,我知道它们会触发路由器,那么我该如何做到这一点?

我找到了下面的 Backbone 路由器原型的脚本,但它更像是一个 hack,而不是一个稳定的解决方案:Simple backbone routing without hash URLs

  • 3) Backbone.js / Marionette.js 中是否可以有子路由器?

我所说的子路由器是指只处理部分 url 的路由器,例如:

var AppRouter = Backbone.Router.extend({
    routes: {
        'articles' : 'MyArticleRouter'
    }
});

var MyArticleRouter = Backbone.Router.extend({
    routes: {
        'science' : 'someMethod',
        'literrature' : 'someOtherMethod'
    }
});

通过让我在 AppRouter 中定义主路由以及在特定类别的子路由器中定义所有子路由(第二个斜杠“/”之后的部分),这将进一步对我的 URL 进行分类。

因此对于以下 URL:“hostname/articles/science”,路由过程将如下所示:

  • 1) 将“/articles/science”传递给 AppRouter
  • 2) AppRouter 拆分 URI 并获取“/articles”部分
  • 3) AppRouter 找到注册的“/articles”路由
  • 4) AppRouter 识别出 MyArticleRouter 已绑定到该 URI 元素
  • 5) AppRouter 将路由转发到该路由器,并且仅将“/science”元素作为路由传递
  • 6) MyArticleRouter 将“/science”路由到 someMethod() 并运行它

提前谢谢你!

【问题讨论】:

    标签: backbone.js routes marionette


    【解决方案1】:

    #1 的答案:

    所有路由都注册在Backbone.history.handlers

    #2 的答案:

    您可以为站点中的每个链接添加处理程序:

    var application = new Backbone.Marionette.Application();
    
    application.addInitializer(function(options) {
        // Add class to target a browser, not as standalone app.
        if(window.navigator.standalone != true) {
            $('body').addClass('no-standalone');
        }
    
        // Prevent internal links from causing a page refresh.
        $(document).on('click', 'a', function(event) {
            var fragment = Backbone.history.getFragment($(this).attr('href'));
            var matched = _.any(Backbone.history.handlers, function(handler) {
                return handler.route.test(fragment);
            });
            if (matched) {
                event.preventDefault();
                Backbone.history.navigate(fragment, { trigger: true });
            }
        });
    });
    

    当然要确保你使用 pushState:

        if (!Backbone.history.started) {
            Backbone.history.start({ pushState: true });
        }
    

    最后一个 sn-p 必须在你初始化所有路由器之后运行。

    #3 的答案:

    这可能有助于拆分您的路线:

    define([
        'backbone',
        'underscore',
        'routers/dashboard',
        'routers/anotherroute1',
        'routers/anotherroute2'
    ],
    
    function(Backbone, _, DashboardRouter, AnotherRoute1, AnotherRoute2) {
        'use strict';
    
        var application = new Backbone.Marionette.Application();
    
        application.addInitializer(function () {
    
            _.each([DashboardRouter, AnotherRoute1, AnotherRoute2], function(router) {
                new router();
            });
    
            if (!Backbone.history.started) {
                Backbone.history.start({ pushState: true });
            }
        });
    
        return application;
    });
    

    【讨论】:

    • 感谢您的回答,我开始认为没有人能够回答。 ^^
    • 对于#3,我不使用子路由,但我确实有超过 1 个在应用程序初始化程序之一上创建的路由。更新了答案。
    • @JulioMenendez 有没有办法让每条路由调用控制器操作?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 2017-12-03
    • 1970-01-01
    • 2019-05-10
    • 2018-06-23
    相关资源
    最近更新 更多