I use SonarLint with Eclipse, and I\'m codding an application using AngularJS. I had a problem with a controller so I was trying to clean it a bit
If you want to remove complexity you can make one function :
$scope.startgenerator = function() {
$http.get('/start').success(function () {
$scope.updateStatus();
});
};
$scope.resumegenerator = function() {
$http.get('/resume').success(function () {
$scope.updateStatus();
});
};
$scope.suspendgenerator = function() {
$http.get('/suspend').success(function () {
$scope.updateStatus();
});
};
$scope.stopgenerator = function() {
$http.get('/stop').success(function () {
$scope.updateStatus();
});
};
to
$scope.generatorAction = function(action) {
$http.get('/' + action).success(function () {
$scope.updateStatus();
});
};
and then use it like this:
$scope.generatorAction('stop');
Or use a service that handle your http request, It's a better practice.
Edit:
I'm using this styleguide for my angular applications : https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md
Creating a simple service for your http request :
(function() {
'use strict';
angular
.module('yourModuleName')
.factory('generator', generatorFactory);
function generatorFactory($http) {
var service = {
start: start,
resume: resume,
suspend: suspend,
stop: stop
}
return service;
function start() {
return $http.get('/start');
}
function resume() {
return $http.get('/start');
}
function suspend() {
return $http.get('/suspend');
}
function stop() {
return $http.get('/stop');
}
}
})();
And then in your controller:
app.controller('LauncherCtrl', function ($scope, generator, $http) {
$scope.genStatus = "stopped";
$scope.startgenerator = function() {
generator.start().then(function () {
$scope.updateStatus();
});
};
$scope.resumegenerator = function() {
generator.resume().then(function () {
$scope.updateStatus();
});
};
$scope.suspendgenerator = function() {
generator.suspend().then(function () {
$scope.updateStatus();
});
};
$scope.stopgenerator = function() {
generator.stop().then(function () {
$scope.updateStatus();
});
};
$scope.updateStatus = function() {
$http.get('/status').success(function (response) {
$scope.genStatus = response.data;
});
};
$scope.updateStatus();
});
First it seems to take more code and more complexity to your app, but if you need to stop your generator in an other page or in a component/directive, you just have to inject your 'generator' service and do generator.stop();
and by doing this, if one day your endpoint url changed, you only have to change them in your service.