我有以下的Javascript文件夹结构:命名约定辅助功能和自我创建的对象
- js
- libs
- Backbone
- Underscore
- Require
- Etc
- models
- templates
- views
- app.js
- main.js
- router.js
为了避免混乱与回调函数前端路由器,最好我想委派的功能向外部模块并且每条路线最多有1行代码。这样我保持一个非常干净的概述,当代理功能发生变化时,我绝不应再实际碰到路由器。
例如:
var Router = Backbone.Router.extend({
/* --- 1. Route management --- */
routes: {
'': 'landing_page',
'(/)login': 'auth_redirect',
'(/)home': 'auth_redirect'
},
landing_page: function(){
this.navigate("/login", {trigger:true});
},
auth_redirect: function(){
//Checks if the user is authenticated;
//Returns either "true" or "false"
$.get('/ingeb/api_v1/auth', _.bind(function(response){
var success = $.parseJSON(response)['success'];
if (success === false){
this.renderView(Login_view);
}else if(success === true){
this.renderView(Home_view);
};
}, this));
}, ...
我想委托处理该认证检查和重定向到外部模块的代码。我想为辅助函数做同样的事情,我可以在整个应用程序中调用静态方法(不需要实例化)。
由于我的文件夹结构现在很干净,我想保持这种方式。
有没有订购这些任何的最佳做法:
- 委托对象;
- 辅助功能;
在干净的文件夹结构?
约曼生成web应用程序也有类似的文件夹结构,它也是一个文件夹命名的帮手,看看http://yeoman.io/gettingstarted .html – mguimard
你有直接链接到文件夹结构的样子吗?不幸的是,我有一个截止日期,现在没有时间通过安装过程。谢谢 – Trace
请看下面的回答 – mguimard