D
D
DimaPolishuk2016-07-27 16:48:13
JavaScript
DimaPolishuk, 2016-07-27 16:48:13

How to get rid of extra js code?

4e8b3cfde0cc4bf09ec30a7bd52b106b.png
I have 2 types of buttons "Start / Stop" and "Quick start"
here are the functions that are responsible for their work
Function for the "Start / Stop" button

var isStart = true;
        $scope.buttonText = "Start";

        $scope.startOrStop = function() {
          if (isStart) {
            $scope.style = {
              background: 'red'
            };
            $scope.start();
            $scope.startTIME();
            isStart = false;
            $scope.buttonText = "Stop";
          } else {
            $scope.style = {
              background: '#11dc51'
            };
            $scope.stop();
            isStart = true;
            $scope.buttonText = "Start";
          }
        };

and this is the function for the "Quick Start" button
$scope.quickStart=function(tasks){
          if (isStart) {
            $scope.style = {
              background: 'red'
            };
            $scope.start();
            $scope.startTIME();
            $scope.tasks.name;
            isStart = false;
            $scope.buttonText = "Stop";
            $scope.currentTask.name=tasks.name;
            $scope.currentTask.selectedProject=tasks.selectedProject;

          } else {
            $scope.style = {
              background: 'red'
            };
            $scope.stop();
            var restart = $timeout($scope.quickStart, 100);
            $scope.currentTask.name=tasks.name;
            $scope.currentTask.selectedProject=tasks.selectedProject;
            isStart = true;
            $scope.buttonText = "Stop";
          }
        }

In general, the code is similar to each other, how to make it so as to reduce the extra code and look more concise?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex, 2016-07-27
@streetflush

1. Instead of styles, add css classes and change them using ng-class
2. Button text can also be changed in html by the value of the variable
3. Duplicate code in if else can be bracketed
4. Why the line $scope.tasks.name;
5. It is better to change the isStart variable in start() and stop()
6. controllerAs
...

A
Anton, 2016-07-27
@SPAHI4

Something like

tasks = {
    START: {
      name: 'Start',
      color: 'green',

    },
    STOP: {
      name: 'Stop',
      color: 'red',

    }
  };

  $scope.currentTask = tasks.STOP;

<button>{{currentTask.name}}</button>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question