2015-10-15 35 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

相關問題