问题描述
我发现大多数教程采用一个大的路由器。
例如:
I found that most tutorials use one big router.For example: https://github.com/thomasdavis/backboneboilerplate/blob/gh-pages/js/router.js
那岂不是更好的路由(控制器)分离成单独的文件?
Wouldn't it be better to separate the routes (controllers) into separate files?
如果是的,我怎么能结合这与requirejs?
If yes how can I combine this with requirejs?
推荐答案
我觉得这是preference的问题。如果你正在做有极大数路由的ginormous的应用程序,然后将您的路由器起来是明智的。对于具有只是一个大的路由器的小型应用程序就好了。
I think this is a question of preference. If you're doing a ginormous application with gazillion routes, then dividing your routers up is sensible. For small applications having just one big router is just fine.
如果您决定有多个路由器,请确保您没有冲突的路线,所以不会有任何意外情况或错误。
If you decide to have multiple routers, make sure you don't have conflicting routes, so there won't be any unexpected behavior or errors.
因此,与requireJS:我认为最好的办法是在自己的文件中像这样来定义每个路由器
So with requireJS: I think the best way would be to define each router in it's own file like this
define([blaa, blaa], function(Blaa, Blaa) {
var SubRouter1 = Backbone.Router.extend({
// work your routing magic here, remember to make no conflicting routes
});
return SubRouter1;
});
当你把所有的希望路由器设置你可以捆绑起来在 app.js
When you have all your desired routers set up you can bundle them up in the app.js
define([...,'subrouter1', 'subrouter2', ... , 'subrouterN', ...],
function(..., SubRouter1, SubRouter2, ... , SubRouterN, ...) {
// work your app magic here
initialize: function() { // or wherever you start your application
subrouter1 = new SubRouter1();
subrouter2 = new SubRouter2();
...
...
subrouterN = new SubRouterN();
Backbone.history.start(); // remember to start the history
},
// maybe work some more magic?
});
我从来没有做过这自己,但我不明白为什么如果保持路线从冲突的这是行不通的。希望这将清除的东西给你。
I've never done this myself, but I don't see why it wouldn't work if you keep the routes from conflicting. Hopefully this clears stuff for you.
这篇关于骨干路由器一般与单独的路由文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!