如何创建枚举,或者应该将枚举存储在何处
How to create an enumeration or where should I store it?
我一直在使用ExtJS,我发现自己使用"魔法"字符串做了很多检查。我想使用某种枚举例如
颜色。红色,颜色。白色的
等Extjs是否支持这个,我使用的是4.2版本。
如果我需要创建一个新的类或其他东西,那么这个正确的位置在哪里?
我现在有
/app
controller
store
models
views
etc
这些似乎不是正确的位置,因为这些是专门为控制器,视图,模型,商店..
这可以有不同的做法,只是为了给你一些启发。
我在我的应用程序中做的是在我的app folder
中创建一个文件夹enums
。在这个文件夹中,我把所有我想在我的应用程序中使用的枚举。请注意,我使用alternateClassName
和uppercase
使它们更像枚举。
只是一个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.js
的requires
数组中使它们全局化:
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
}
}
});
相关文章:
- 如何发送分配列表<字符串>到JavaScript数组或可枚举对象
- 枚举附加到文档的HTML对象的所有事件
- JavaScript数组的子类化破坏了快速枚举
- 为什么Javascript程序员更喜欢在代码中重复字符串作为常量,而不是使用枚举类型的对象
- 不可枚举的属性出现在 for..在 Chrome 中循环
- json模式v4如何根据枚举实现枚举
- 如何访问从 jstl foreach 到 jQuery 的枚举值
- 如何从 Javascript 访问 Java 枚举 (Java 1.8)
- JSON.stringify未序列化可枚举属性
- 为什么ES6 WeakMap's不可枚举
- 如何在另一个对象中枚举一个对象
- Javascript propertyIsNumerable-是否有可枚举的方法
- Javascript不可枚举属性
- TypeScript 中的枚举:JavaScript 代码在做什么
- 使用枚举作为类型/接口
- sails.js:枚举的模型定义 - 动态值列表
- 如何更改 Javascript 对象属性的可写可枚举和可配置值
- 如何将中继变量设置为枚举值
- 枚举本地存储属性
- 如何创建枚举,或者应该将枚举存储在何处