BackboneJs 中的多个路由器与单个路由器 [英] Multiple routers vs single router in BackboneJs

查看:40
本文介绍了BackboneJs 中的多个路由器与单个路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过的 Backbone 上的所有示例都为整个应用程序使用一个路由器,但是为应用程序的每个部分(页眉、页脚、舞台、侧边栏)都设置一个路由器是否有意义?有没有人用不止一个路由器开发过应用?您有什么体验?

All examples on Backbone I've seen use one router for the whole application, but wouldn't it make sense to have a router for each single part of your app (header, footer, stage, sidebar)? Has anyone built apps with more than one router and what are your experiences?

让我们考虑一个带有嵌套视图的复杂应用程序:当一个视图有自己的路由器来处理子视图的显示时,这不是比拥有一个必须通知主视图更改其子视图的大路由器更好吗??

Let's think about a complex app with nested views: Wouldn't it be better when a view has its own router that handles the display of the subviews, than having one big router that has to inform the main view to change its subviews?

这个问题的背景:我看到了主干中的路由器和 GWT 中的 ActivityMapper 有很多相似之处.ActivityMapper 只负责为 DOM 中的给定路由和给定容器获取正确的 Presenter.

The background of this question: I've see a lot of parallels of the router in backbone and the ActivityMapper in GWT. The ActivityMapper is only responsible to get the right presenter for a given route and a given container in the DOM.

推荐答案

我编写了一个应用程序(仍在编写),其中包含多个路由器.然而它并不像你想象的那样,它更多地基于模块,而不是每个视图的路由器或类似的东西.

i wrote an app (still writing) with multiple routers in it. however it is not like you think, it is more module based and not a router per view or anything like that.

例如,假设我的应用程序中有两个大模块,1 个处理所有书籍,1 个用于用户.书籍有多个视图(和用户一样)、列表视图、详细信息视图、编辑视图等...所以每个模块都有自己的路由器,代表它自己的一组网址:

for example, say i got two big modules in my app, 1 handling all books, and 1 for the users. books have multiple views (as do users), a list view, detail view, edit view, etc etc... so each module has its own router, which stands for its own set of urls:

// user module...
var userRouter = Backbone.Router.extend({
    routes: {
        "users": "loadUsers",
        "users/add": "addUser",
        "user/:id": "loadUser",
        "user/:id/edit": "editUser"
    }

// ... rest dropped to save the trees (you never know if someone prints this out)
});


// book module
var bookRouter = Backbone.Router.extend({
    routes: {
        "books": "loadBooks",
        "books/add": "addBook",
        "book/:name": "loadBook",
        "book/:name/edit": "editBook"
    }

// ... rest dropped to save the trees (you never know if someone prints this out)
});

所以,我的两个路由器并不是在竞争同一条路由,它们各自处理自己的一组路由.

so, it is not like my two routers are competing for the same route, they each handle their own set of routes.

编辑现在我通过 Elf Sternberg 获得了更多信息,我知道默认情况下不可能在同一条路由上匹配多个路由器.没有像覆盖主干历史或在路由和正则表达式中使用命名空间来匹配这些路由的解决方法.更多信息:多个匹配路由感谢 Elf Sternberg 提供链接.

edit now that I had more info via Elf Sternberg, I know it isn't possible by default to have multiple routers match on the same route. without a workaround like overriding the backbone history or using namespaces in routes and regexes to match these routes. more info here: multiple matching routes thanks Elf Sternberg for the link.

这篇关于BackboneJs 中的多个路由器与单个路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆