如何创建枚举,或者应该将枚举存储在何处

How to create an enumeration or where should I store it?

本文关键字:枚举 存储 在何处 或者 创建 何创建      更新时间:2023-09-26

我一直在使用ExtJS,我发现自己使用"魔法"字符串做了很多检查。我想使用某种枚举例如

颜色。红色,颜色。白色的

Extjs是否支持这个,我使用的是4.2版本。

如果我需要创建一个新的类或其他东西,那么这个正确的位置在哪里?

我现在有

  /app
   controller
   store
   models
   views
    etc

这些似乎不是正确的位置,因为这些是专门为控制器,视图,模型,商店..

这可以有不同的做法,只是为了给你一些启发。

我在我的应用程序中做的是在我的app folder中创建一个文件夹enums。在这个文件夹中,我把所有我想在我的应用程序中使用的枚举。请注意,我使用alternateClassNameuppercase使它们更像枚举。

只是一个enum:

Ext.define('MyApp.enums.Orientation', {
    alternateClassName: ['ORIENTATION'],
    statics: {
        PORTRAITPRIMARY: 'portrait-primary', // The orientation is in the primary portrait mode.
        PORTRAITSECONDARY: 'portrait-secondary', // The orientation is in the secondary portrait mode.
        LANDSCAPEPRIMARY: 'landscape-primary', // The orientation is in the primary landscape mode.
        LANDSCAPESECONDARY: 'landscape-secondary', // The orientation is in the secondary landscape mode.
        PORTRAIT: 'portrait', // The orientation is either portrait-primary or portrait-secondary.
        LANDSCAPE: 'landscape' // The orientation is either landscape-primary or landscape-secondary.
    }
});

我可以这样使用:

MyApp.util.CordovaPlugins.lockOrientation(ORIENTATION.LANDSCAPE);

其中lockOrientation看起来像这样:

/**
 * Lock the viewport in a certain orientation and disallow rotation using the cordova screen orientation plugin
 * See [github.com/gbenvenuti/cordova-plugin-screen-orientation](https://github.com/gbenvenuti/cordova-plugin-screen-orientation)
 * for more details.
 *
 * Usage:
 * MyApp.util.CordovaPlugins.lockOrientation(ORIENTATION.LANDSCAPE);
 *
 * Possible orientations:
 * ORIENTATION.PORTRAITPRIMARY
 * ORIENTATION.PORTRAITSECONDARY
 * ORIENTATION.LANDSCAPEPRIMARY
 * ORIENTATION.LANDSCAPESECONDARY
 * ORIENTATION.PORTRAIT
 * ORIENTATION.LANDSCAPE
 *
 * @param {Enum} orientation Value of type MyApp.enums.Orientation to orientate the view in the given orientation.
 */
lockOrientation: function(orientation) {
    if (ORIENTATION.hasOwnProperty(orientation.toUpperCase())) {
        screen.lockOrientation(orientation);
    }
    else {
        Ext.Logger.error('The given orientation is not prohibited.');
    }
}

另一个枚举:

Ext.define('MyApp.enums.PositionError', {
    alternateClassName: ['POSITIONERROR'],
    statics: {
        PERMISSION_DENIED: 1,
        POSITION_UNAVAILABLE: 2,
        TIMEOUT: 3
    }
});

用法:

getGpsErrorTitleByErrorCode: function(errorCode) {
    var title;
    switch (errorCode) {
        case POSITIONERROR.PERMISSION_DENIED:
            title = 'PERMISSION_DENIED';
            break;
        case POSITIONERROR.POSITION_UNAVAILABLE:
            title = 'POSITION_UNAVAILABLE';
            break;
        case POSITIONERROR.TIMEOUT:
            title = 'TIMEOUT';
            break;
        default:
            title: 'UNKNOWN_ERROR';
            break;
    }
    return title;
}

我将枚举添加到我使用enum的类中的uses数组:

Ext.define('MyApp.util.CordovaPlugins', {
    uses: [
        'MyApp.enums.PositionError',
        'MyApp.enums.Orientation'
    ],
    ...
});

或在app.jsrequires数组中使它们全局化:

Ext.application({
    name: 'MyApp',
    requires: [
        'MyApp.enums.*'
    ],
    ...
});

ExtJS只是一个JavaScript框架,所以你可以在JavaScript中做的任何事情都可以在Ext中做。

在文件夹结构方面,除了你所描述的商店、模型等的惯例外,这纯粹是个人偏好。我建议使用一个足够通用的结构来应用于几个项目,这样你就可以在需要的时候迁移结构,或者在项目之间移动方面而不需要把它们硬塞进去。

对于Colours.Red这样的东西,你可以通过普通的JS来实现,所以也许像这样的对象:

var Colours = {
    Black: "#000000",
    White: "#FFFFFF"
};

要以更简洁的方式完成此操作,您将看到如下内容:

Ext.define('MyApp.model.Util', {
   statics: {
      Colours: {
         Black: 1, 
         White: 2
      }
    }
});