Это можно сделать по-другому, и это только для того, чтобы дать вам некоторое вдохновение.
Что я сделал в своем приложении, это создание папки enums
в моем app folder
. В этой папке я помещаю все мои перечисления, которые я хочу использовать в своем приложении. Имейте в виду, что я использую 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