2015-10-15 62 views
0

我一直在使用ExtJS,我发现自己使用“魔术”字符串进行了很多检查。我想用某种枚举即如何创建一个枚举或我应该在哪里存储它?

Colors.Red,Colors.White

ExtJS的是否支持这一点,我使用的4.2版。

此外,如果我需要创建一个新的类或什么的话,那么这个地方会是什么地方?

我现在有

/app 
    controller 
    store 
    models 
    views 
    etc 

这些似乎并没有被正确的位置,因为这些都是专门为控制器,视图,模型,商店..

哪里是最好的地方创造的东西那不符合上述?

回答

2

这可以做不同的事情,它只是给你一些启发。

我在我的应用程序中所做的是在我的app folder中创建一个文件夹enums。在这个文件夹中,我把我想要在我的应用程序中使用的所有枚举。请注意,我使用alternateClassNameuppercase使它们更像枚举。

只是一个枚举:

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.*' 
    ], 

    ... 
}); 
+0

不错,灵感收到:-) ...是的,让我思考,并开始了解我的选择!谢谢。 – Martin

0

ExtJS只是一个JavaScript框架,所以你可以在JavaScript中做任何事你可以在Ext中做。

就文件夹结构而言,它完全是个人偏好,除了他们针对商店,模型等的惯例外,正如您所描述的那样。 我建议使用一个结构,它是通用的,足以应用于多个项目,使您可以迁移结构如果需要的话,或将跨项目方面,而无需硬塞进他们进来。

在像Colours.Red物联网方面,再次,你可以通过正常的JS做到这一点,所以也许对象是这样的:

var Colours = { 
    Black: "#000000", 
    White: "#FFFFFF" 
}; 

要更Ext'y的方式做到这一点,你要寻找的东西,如:

Ext.define('MyApp.model.Util', { 
    statics: { 
     Colours: { 
     Black: 1, 
     White: 2 
     } 
    } 
}); 
+0

这不是特别有用。是的,我知道这是纯JavaScript的方式。我找到了在application.js中使用“require”并“定义”一个类并将其标记为单例的方法。 – Martin

+0

文件夹结构是个人偏好,但我希望从别人正在做的一些输入。就这些。 – Martin

+0

如果您知道创建单例或static'esque类的方法,以避免您的颜色示例使用魔术字符串,并且您正在寻找关于在何处存储实用工具类的主观建议,我不确定是什么答案会有帮助。 – dougajmcdonald

相关问题