1

How can i optimise the duplicate code here

angular.module('myApp')
    .controller('LogsController', function ($scope, LogsService) {
        $scope.updatingLogs = true;
        $scope.loggers = {};

        LogsService.findAll().$promise.then(function(data) {
            $scope.loggers = data;
            $scope.updatingLogs = false;
        });

        $scope.changeLevel = function (name, level) {
            LogsService.changeLevel({name: name, level: level}, function () {
                $scope.updatingLogs = true;
                LogsService.findAll().$promise.then(function(data) {
                    $scope.loggers = data;
                    $scope.updatingLogs = false;
                });
            });
        };
    });

3 Answers 3

1

Here is a suggestion:

angular.module('myApp')
    .controller('LogsController', function ($scope, LogsService) {
        $scope.updatingLogs;
        $scope.loggers = LogsService.findAll();

        $scope.changeLevel = function (name, level) {
            $scope.updatingLogs = LogsService.changeLevel({name: name, level: level}, function () {
                LogsService.findAll().$promise.then(function(data) {
                    $scope.loggers = data;
                    $scope.updatingLogs = null;
                });
            });
        };
    });
Sign up to request clarification or add additional context in comments.

Comments

0

You could do create $scope.findAll method which will make an ajax of LogsService.findAll() so that you could be utilize this function while doing it multiple times from elsewhere.Optimize version would be.

Code

angular.module('myApp')
    .controller('LogsController', function($scope, LogsService) {
        $scope.updatingLogs = true;
        $scope.loggers = {};

        $scope.findAll = function() {
            LogsService.findAll().$promise.then(function(data) {
                $scope.loggers = data;
                $scope.updatingLogs = false;
            });
        }

        $scope.changeLevel = function(name, level) {
            LogsService.changeLevel({
                name: name,
                level: level
            }, function() {
                $scope.updatingLogs = true;
                $scope.findAll();
            });
        };

        $scope.findAll(); //init
    });

2 Comments

this is really close to what I was about to say. The difference was that findAll() wasn't in $scope, unless the view needed to call it directly.
@fractalspawn ya..i added it in $scope because either way we can initialize it using ng-init which would make more sense as per unit testing perspective
0

Depends on what you're reaching for. If you're looking for code clarity, a potential refactor might look something like this.

angular.module('myApp')
  .controller('LogsController', function ($scope, LogsService) {

    activate();

    $scope.changeLevel = function (name, level) {
        LogsService.changeLevel({name: name, level: level}, changelevelHandler);
    }; 

    function changeLevelHandler() {
        $scope.updatingLogs = true;
        getLogs();
    }

    function getLogs() {
      LogsService.findAll().$promise.then(function(data) {updateLoggers(data);});
    }

    function updateLoggers(data) {
        $scope.loggers = data;
        $scope.updatingLogs = false;
    }

    function activate() {
       $scope.updatingLogs = true;
       $scope.loggers = {};
       getLogs();

    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.