/*
eslint no-shadow: ["error", { "allow": ["$scope"] }]
*/
/*
eslint no-underscore-dangle: ["error", { "allow": ["_id",] }]
*/

const ideaControl = function ideaControl($scope, $mdDialog, $mdToast, db) {
  $scope.isShown = true;
  $scope.current = undefined;

  $scope.remove = (idea) => {
    db.removeIdea(idea, (err) => {
      if (!err) {
        $mdToast.show(
          $mdToast.simple()
          .textContent('Idea deleted.')
          .position('bottom start')
          .hideDelay(1000)
        );
      }
    });
  };

  function EditDialogController($scope, idea) {
    $scope.idea = idea;
    $scope.cancel = function cancel() {
      $mdDialog.cancel();
    };
    $scope.update = function update(newIdea) {
      if (newIdea) {
        $mdDialog.hide(newIdea);
      }
    };
  }
  $scope.edit = (ev, cur) => {
    $mdDialog.show({
      controller: EditDialogController,
      locals: {
        idea: cur.idea,
      },
      templateUrl: 'app/components/ideas/templates/editIdeaDialog.html',
      parent: angular.element(document.body),
      targetEvent: ev,
      clickOutsideToClose: true,
    })
    .then((idea) => {
      db.editIdea(cur._id, idea, (err) => {
        if (!err) {
          db.findIdeas((ideas) => {
            $scope.ideas = ideas;
            $scope.$apply();
          });
          $mdToast.show(
            $mdToast.simple()
            .textContent('Idea edited.')
            .position('bottom start')
            .hideDelay(1000)
          );
        }
      });
    });
  };

  function SplitDialogController($scope) {
    $scope.tasks = [];
    $scope.numOfTasks = 1;
    $scope.cancel = function cancel() {
      $mdDialog.cancel();
    };
    $scope.split = function update(splittedTasks, numOfTasks) {
      if (splittedTasks.length === numOfTasks) {
        $mdDialog.hide(splittedTasks);
      }
    };
  }
  $scope.split = (ev) => {
    $mdDialog.show({
      controller: SplitDialogController,
      templateUrl: 'app/components/ideas/templates/splitIdeaDialog.html',
      parent: angular.element(document.body),
      targetEvent: ev,
      clickOutsideToClose: true,
    })
    .then((tasks) => {
      // db.editIdea(cur._id, idea, (err) => {
      //   if (!err) {
      //     db.findIdeas((ideas) => {
      //       $scope.ideas = ideas;
      //       $scope.$apply();
      //     });
      //     $mdToast.show(
      //       $mdToast.simple()
      //       .textContent('Idea edited.')
      //       .position('bottom start')
      //       .hideDelay(1000)
      //     );
      //   }
      // });
    });
  };
  $scope.$on('Update ideas', () => {
    db.findIdeas((ideas) => {
      $scope.ideas = ideas;
    });
  });
  db.findIdeas((ideas) => {
    $scope.ideas = ideas;
    $scope.$apply();
  });
};

angular.module('MainApp')
  .controller('IdeaControl', ideaControl);

@@ -169,16 +169,28 @@
 ($scope,
+ $rootScope,
  $mdDial
@@ -2174,36 +2174,82 @@
     
-// db.editIdea(cur._id, idea
+let hasErr = false;%0A      tasks.forEach((task) =%3E %7B%0A        db.insert(task
 , (e
@@ -2267,18 +2267,16 @@
     
-//
+ 
    if (
-!
 err)
@@ -2288,84 +2288,59 @@
     
-//
      
-db.findIdeas((ideas) =%3E %7B
+ hasErr = true;
 %0A      
-//
     
+%7D%0A
    
-$scope.ideas = ideas
+     %7D)
 ;%0A      
 //  
@@ -2339,59 +2339,83 @@
     
-//
+%7D);%0A
       
- $scope.$apply();%0A      //     %7D
+if (!hasErr) %7B%0A        $rootScope.$broadcast('Update tasks'
 );%0A   
-   //
     
@@ -2429,29 +2429,24 @@
 st.show(%0A   
-   //
        $mdTo
@@ -2457,29 +2457,24 @@
 simple()%0A   
-   //
        .text
@@ -2491,26 +2491,34 @@
 dea 
-edited
+splitted into tasks
 .')%0A   
-   //
     
@@ -2556,75 +2556,20 @@
     
-//       .hideDelay(1000)%0A      //     );%0A      //   %7D%0A      // %7D);
+  );%0A      %7D
 %0A   

Add splitted tasks into database