我一直在使用 ExtJS,我发现自己使用“神奇”字符串进行了大量检查。我想使用某种枚举,即
颜色.红色,颜色.白色
等等
Extjs支持这个吗,我用的是4.2版本。
此外,如果我需要创建一个新类或其他东西,那么正确的位置在哪里?
我目前有
/app
controller
store
models
views
etc
这些似乎不是正确的位置,因为它们是专门用于 Controller 、 View 、模型、存储的..
哪里是创建不符合上述内容的内容的最佳位置?
最佳答案
这可以用不同的方式完成,这只是为了给您一些灵感。
我在应用程序中所做的就是在我的应用程序文件夹
中创建一个文件夹enums
。在这个文件夹中,我放置了我想在应用程序中使用的所有枚举。请注意,我使用 alternateClassName
和 uppercase
使它们更像枚举。
只是一个枚举:
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;
}
我将枚举添加到使用枚举的类中的 uses
数组中:
Ext.define('MyApp.util.CordovaPlugins', {
uses: [
'MyApp.enums.PositionError',
'MyApp.enums.Orientation'
],
...
});
或者在 app.js
的 requires
数组中使它们全局化:
Ext.application({
name: 'MyApp',
requires: [
'MyApp.enums.*'
],
...
});
关于javascript - 如何创建枚举或应该将其存储在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33142413/