這可以做不同的事情,它只是給你一些啓發。
我在我的應用程序中所做的是在我的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