问题描述
使用一台路由器为整个应用程序所有骨干的例子。但不会是有意义的为您的应用程序(页眉,页脚,舞台,边栏)的一个部分的路由器。所以我的问题是有没有人构建的应用程序与多个路由器,什么是经验。
All example on backbone using one router for the whole application. But wouldn't it make sense to have a router for single part of your app (header, footer, stage, sidebar). So my question is has anyone build apps with more than one router and what are experience.
让我们想想有嵌套视图一个复杂的应用程序。那岂不是更好,当一个视图有一个自己的路由器处理子视图的显示,比拥有一个大的路由器,它必须通知主视图改变自己的看法子?
Lets think about a complex app with nested views. Wouldn't it be better when a view have an own router that handle the display of the subviews, than having one big router that have to inform the main view to change his sub views?
这个问题的背景:我已经看到了很多骨干路由器和GWT的ActivityMapper的相似之处。该ActivityMapper只负责以获得正确的presenter对于给定的路线和DOM中的给定的容器。
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.
修改
现在,我通过精灵斯腾有更多的信息,我知道这是不是在默认情况下可能具有相同的路线多个路由器匹配。没有像覆盖骨干历史或使用命名空间中的路线和正则表达式匹配这些路由的解决方法。
这里更多的信息:
感谢精灵斯腾伯格的链接。
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.
这篇关于多个路由器VS在BackboneJs台路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!