如何创建枚举或我应该将它存储在哪里?

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

我一直在使用 ExtJS,我发现自己使用 "magic" 字符串进行了大量检查。我想使用某种枚举,即

Colors.Red, Colors.White

等等

Extjs支持吗,我用的是4.2版本

此外,如果我需要创建一个新的 class 或其他内容,那么正确的位置应该在哪里?

我目前有

  /app
   controller
   store
   models
   views
    etc

这些似乎不是正确的位置,因为它们专门用于控制器、视图、模型、存储..

创建不符合上述要求的内容的最佳位置在哪里?

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

中做

就文件夹结构而言,除了您所描述的商店、模型等的约定外,这纯粹是个人偏好。 我建议使用一个足够通用的结构来应用于多个项目,这样您就可以在需要时迁移结构,或者在项目之间移动方面而无需强行插入。

Colours.Red 之类的事情而言,您同样可以通过普通 JS 执行此操作,因此也许是这样的对象:

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

要以更 Ext'y 的方式做到这一点,您正在寻找类似的东西:

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

这个可以有不同的做法,只是给大家一些启发

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

只是一个枚举:

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;
}

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

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

    ...
});

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

Ext.application({
    name: 'MyApp',

    requires: [
        'MyApp.enums.*'
    ],

    ...
});