javascript BackboneJs 中的多个路由器与单个路由器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8362230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:12:14  来源:igfitidea点击:

Multiple routers vs single router in BackboneJs

javascriptarchitecturebackbone.jsrouter

提问by Andreas K?berle

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?

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

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?

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

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.

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

采纳答案by Sander

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.

我写了一个应用程序(还在写),里面有多个路由器。然而它并不像你想象的那样,它更多地基于模块,而不是每个视图的路由器或类似的东西。

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:

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

// 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.

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

editnow 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 routesthanks Elf Sternberg for the link.

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

回答by Dave Cadwallader

I just wrote a blog post on Module-Specific Subroutes in Backbone, which allow a "subroute" to be defined which pays attention to everything afterthe prefix for that route.

我刚刚写了一篇关于 Backbone 中模块特定子路由的博客文章,它允许定义一个“子路由”,它关注该路由前缀之后的所有内容。

Check out the blog entry for more explanation: http://www.geekdave.com/?p=13

查看博客条目以获得更多解释:http: //www.geekdave.com/?p=13

This means you don't have to redundantly define the same prefix over and over, and you can also lazy-load subroutes as modules are accessed. Feedback is most welcome!

这意味着您不必一遍又一遍地重复定义相同的前缀,并且您还可以在访问模块时延迟加载子路由。非常欢迎反馈!

回答by Pallavi Anderson

There is a limited but important case when it makes sense to use multiple Routers. If you need to expose only a subset of your application's routes & views based on data collected at runtime (perhaps login credentials - e.g., manager vs. staff can see & navigate between different sets of views) you could instantiate only the appropriate Router & View classes. This is significant because routes can be bookmarked and sent from user to user. Of course, you still need checks on the server to ensure that an unauthorized user isn't issuing requests after navigating to a view they arrived at via a bookmark sent by an authorized user. But it's better to design the application so the unauthorized view is just not generated.

当使用多个路由器有意义时,有一个有限但重要的情况。如果您只需要根据运行时收集的数据公开应用程序的路由和视图的子集(可能是登录凭据 - 例如,经理与员工可以在不同的视图集之间查看和导航),您可以仅实例化适当的路由器和视图类。这很重要,因为可以为路由添加书签并从用户发送到用户。当然,您仍然需要检查服务器以确保未经授权的用户在导航到他们通过授权用户发送的书签到达的视图后不会发出请求。但是最好将应用程序设计为不生成未经授权的视图。