2016-11-04 23 views
0

我正在根據功能對我的electronjs(angular,mysql)應用程序文件進行分組,例如,我在開發庫存系統,這就是我如何組織我的文件:我的電子網絡應用程序中的每個功能的app.js文件

/app 

    /scripts 

     /categories 
      -- app.js 
      -- category.html 
      -- categoryController.js 
      -- categoryService.js (MySQL data access procedures) 

    /brands 
      -- app.js 
      -- brands.html 
      -- brandsController.js 
      -- brandsService.js (MySQL data access procedures) 

    /unitsofmeasure 
      -- app.js 
      -- unitsofmeasure.html 
      -- unitsofmeasureController.js 
      -- unitsofmeasureService.js (MySQL data access procedures) 

    /products 
    /suppliers 
     .... 

等等每個功能。

裏面每個app.js文件(例如對於類):

(function() { 
    'use strict'; 

    var _templateBase = './scripts';  

    angular.module('app', [ 
     'ngRoute', 
     'ngMaterial', 
     'ngAnimate'   
    ]) 
    .config(['$routeProvider', function ($routeProvider) { 
      $routeProvider 
      .when('/', { 
       templateUrl: _templateBase + '/category/category.html' , 
       controller: 'categoryController', 
       controllerAs: '_ctrl' 
      }); 
      $routeProvider.otherwise({ redirectTo: '/' }); 
     } 
    ]) ;    
})(); 

我的問題:是否確定有幾個app.js文件的每個功能我的應用程序將有?

+0

我也在開發一個使用Electron的類似庫存系統。你有任何參考項目的網址嗎? – uncivilized

回答

0

嘗試遵循DRY練習或不要重複自己。因此,最好的方法是在所有子文件夾的這些文件夾之外創建一個模塊。我的意思是你創建module.js或任何你想在腳本文件夾的根目錄中命名它。在module.js裏面,你可以創建一個稍後使用的模塊,或者你可以包含所有的配置。

第一種方式:

(function() { 
    'use strict'; 

    angular.module('app', [ 
     'ngRoute', 
     'ngMaterial', 
     'ngAnimate'   
    ]) 

})(); 

,然後將每個文件夾中的文件路由(也許categoriesConfig.js ...)內:

​​

方式二:

(function() { 
    'use strict'; 

    var _templateBase = './scripts';  

    angular.module('app', [ 
     'ngRoute', 
     'ngMaterial', 
     'ngAnimate'   
    ]) 
    .config(['$routeProvider', function ($routeProvider) { 
      $routeProvider 
      .when('/category', { 
       templateUrl: _templateBase + '/category/category.html' , 
       controller: 'categoryController', 
       controllerAs: '_ctrl' 
      }); 
      $routeProvider.otherwise({ redirectTo: '/' }); 
     } 
    ]) ;  
    .config(['$routeProvider', function ($routeProvider) { 
      $routeProvider 
      .when('/brands', { 
       templateUrl: _templateBase + '/brands/brands.html' , 
       controller: 'brandsController', 
       controllerAs: '_ctrl' 
      }); 
      $routeProvider.otherwise({ redirectTo: '/' }); 
     } 
    ]) ;   
})(); 
0

Depends中你在這樣的結構組織中試圖達到什麼目的。如果您想讓與組件相關的路由接近相關組件,那麼添加具有路由到每個文件夾的配置就足夠了(您的第一個示例)。在這種情況下,您不會複製所有模塊中顯示的依賴關係。

例1

angular.module('app') 
    .config(['$routeProvider', function ($routeProvider) { 
     $routeProvider 
     .when('/', { 
      templateUrl: _templateBase + '/category/category.html' , 
      controller: 'categoryController', 
      controllerAs: '_ctrl' 
     }); 
     $routeProvider.otherwise({ redirectTo: '/' }); 
    } 
]) ; 

而如果你的組件可以在未來的一些子成長。您可能需要爲每個模塊創建子模塊,並將配置附加到它們。

例2

angular.module('app.brands', []) 
    .config(['$routeProvider', function ($routeProvider) { 
     $routeProvider 
     .when('/', { 
      templateUrl: _templateBase + '/category/category.html' , 
      controller: 'categoryController', 
      controllerAs: '_ctrl' 
     }); 
     $routeProvider.otherwise({ redirectTo: '/' }); 
    } 
]) ; 

我個人的感覺,那第一個例子將工作最適合你。但是你最好知道你的應用的領域,所以選擇你自己。無論如何,你應該儘量減少所有項目中呈現的依賴關係注入。

相關問題