我們正在嘗試應用John Papa的AngularJS Style Guide中列出的準則。強制推遲控制器邏輯
其中一個,我們就開始遵循的規則是Defer Controller Logic:
Defer logic in a controller by delegating to services and factories.
Why?: Logic may be reused by multiple controllers when placed within a service and exposed via a function.
Why?: Logic in a service can more easily be isolated in a unit test, while the calling logic in the controller can be easily mocked.
Why?: Removes dependencies and hides implementation details from the controller.
這事通過將數據檢索邏輯到控制器中,而不是在服務隔離它,我們已經在過去的侵犯。
現在我想使規則儘可能嚴格。理想情況下,如果其中一個配置的服務作爲依賴項傳遞給控制器,我希望角度引發錯誤。它是可以在角度水平上解決的問題,還是我應該嘗試單獨解決 - 例如,靜態地使用自定義規則ESlint
?
希望得到任何見解或提示。
特別是以下控制器違反了規則,因爲它使用$http
service直接:
function OrderController($http, $q, config, userInfo) {
var vm = this;
vm.checkCredit = checkCredit;
vm.isCreditOk;
function checkCredit() {
var settings = {};
return $http.get(settings)
.then(function(data) {
vm.isCreditOk = vm.total <= maxRemainingAmount;
})
.catch(function(error) {
});
};
}
另外,讓我知道如果我越來越過分關注/瘋狂的代碼質量: )
我會嘗試使用ESLint並使用防止在控制器中使用$ http/$資源的規則。我個人認爲你應該在你的開發團隊中進行交談,並且在處理數據時讓每個人都在同一頁面上。 –