javascript - How to create an enumeration or where should I store it? -
i have been using extjs , find myself doing lot of checking using "magic" strings. use sort of enumeration i.e
colors.red, colors.white
etc
does extjs support this, using version 4.2.
also if need create new class or correct place ?
i have
/app controller store models views etc
these don't seem correct location these controllers, views, models, stores ..
where advisable place create things don't fit above ?
this can done differently, , it's give inspiration.
what have done in app creating folder enums
in app folder
. in folder put enums want use in app. aware use alternateclassname
, uppercase
make them more enum-like.
just enum:
ext.define('myapp.enums.orientation', { alternateclassname: ['orientation'], statics: { portraitprimary: 'portrait-primary', // orientation in primary portrait mode. portraitsecondary: 'portrait-secondary', // orientation in secondary portrait mode. landscapeprimary: 'landscape-primary', // orientation in primary landscape mode. landscapesecondary: 'landscape-secondary', // orientation in secondary landscape mode. portrait: 'portrait', // orientation either portrait-primary or portrait-secondary. landscape: 'landscape' // orientation either landscape-primary or landscape-secondary. } });
i can use this:
myapp.util.cordovaplugins.lockorientation(orientation.landscape);
where lockorientation
looks this:
/** * lock viewport in orientation , disallow rotation using cordova screen orientation plugin * see [github.com/gbenvenuti/cordova-plugin-screen-orientation](https://github.com/gbenvenuti/cordova-plugin-screen-orientation) * 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 orientate view in given orientation. */ lockorientation: function(orientation) { if (orientation.hasownproperty(orientation.touppercase())) { screen.lockorientation(orientation); } else { ext.logger.error('the given orientation not prohibited.'); } }
another enum:
ext.define('myapp.enums.positionerror', { alternateclassname: ['positionerror'], statics: { permission_denied: 1, position_unavailable: 2, timeout: 3 } });
usage:
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; }
i add enums uses
array in class use enum:
ext.define('myapp.util.cordovaplugins', { uses: [ 'myapp.enums.positionerror', 'myapp.enums.orientation' ], ... });
or in requires
array of app.js
make them globally:
ext.application({ name: 'myapp', requires: [ 'myapp.enums.*' ], ... });
Comments
Post a Comment