帮助程序函数和自创建对象的命名约定

Naming convention for helper functions and self created objects

本文关键字:命名约定 创建对象 函数 帮助程序      更新时间:2023-09-26

我有以下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)); 
    }, ... 

我想将处理身份验证检查和重定向到外部模块的代码委托给外部模块。我想对可以在整个应用程序中作为静态方法(无需实例化)调用的帮助程序函数执行相同的操作。

由于我的文件夹结构现在非常干净,我想保持这种方式。

是否有任何最佳实践来订购这些:

  • 委托对象;
  • 辅助功能;

在干净的文件夹结构中?

下面是 yeoman 生成的应用文件夹层次结构的样子

.
├── bower_components
├── images
├── scripts
│   ├── collections
│   ├── helpers
│   ├── lib
│   ├── models
│   ├── routes
│   ├── templates
│   ├── vendor
│   ├── views
│   └── main.js
└── styles
    └── fonts