这可以做不同的事情,它只是给你一些启发。
我在我的应用程序中所做的是在我的app folder
中创建一个文件夹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'
],
...
});
还是requires
阵列的app.js
中,使它们在全球范围:
Ext.application({
name: 'MyApp',
requires: [
'MyApp.enums.*'
],
...
});
不错,灵感收到:-) ...是的,让我思考,并开始了解我的选择!谢谢。 – Martin