Просмотр исходного кода

Ask for confirmation on deletion

Johannes Zellner 11 лет назад
Родитель
Сommit
59cc0cdff7
2 измененных файлов с 37 добавлено и 3 удалено
  1. 15 1
      public/index.html
  2. 22 2
      public/index.js

+ 15 - 1
public/index.html

@@ -64,6 +64,20 @@
   </div>
 </div>
 
+<div class="modal fade" id="modalDel">
+  <div class="modal-dialog">
+    <div class="modal-content">
+      <div class="modal-header">
+        <h4 class="modal-title">Really Delete this Thing?</h4>
+      </div>
+      <div class="modal-footer">
+        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
+        <button type="button" class="btn btn-danger" ng-click="delThing()">Delete</button>
+      </div>
+    </div>
+  </div>
+</div>
+
 <div class="modal fade" id="modalSettings">
   <div class="modal-dialog">
     <div class="modal-content">
@@ -121,7 +135,7 @@
       <div class="well well-sm thing" ng-repeat="thing in things">
           <div class="col-md-12 thing-title-date">
             {{thing.createdAt | prettyDateOffset}}
-            <button class="btn btn-xs btn-default" ng-click="deleteThing(thing._id)">delete</button>
+            <button class="btn btn-xs btn-default" ng-click="showDelThing(thing._id)">delete</button>
             <button class="btn btn-xs btn-default" ng-click="showEditThing(thing._id, thing.content)">edit</button>
         </div>
         <div class="content" marked="thing.richContent"></div>

+ 22 - 2
public/index.js

@@ -28,6 +28,11 @@ app.controller('MainController', function ($scope, $http, $timeout, $document) {
         content: ''
     };
 
+    $scope.delFormData = {
+        busy: true,
+        id: ''
+    };
+
     $scope.settings = {
         title: 'Holy Guacamoly',
         backgroundUrl: ''
@@ -53,6 +58,13 @@ app.controller('MainController', function ($scope, $http, $timeout, $document) {
         $('#modalEdit').modal('show');
     };
 
+    $scope.showDelThing = function (id) {
+        $scope.delFormData.busy = false;
+        $scope.delFormData.id = id;
+
+        $('#modalDel').modal('show');
+    };
+
     $scope.addThing = function () {
         $scope.addFormData.busy = true;
 
@@ -88,11 +100,19 @@ app.controller('MainController', function ($scope, $http, $timeout, $document) {
         });
     };
 
-    $scope.deleteThing = function (id) {
-        $http.delete('/api/things/' + id, {}).then(function (result) {
+    $scope.delThing = function (id) {
+        $scope.delFormData.busy = true;
+
+        $http.delete('/api/things/' + $scope.delFormData.id, {}).then(function (result) {
+            $scope.delFormData.busy = false;
+            $scope.delFormData.id = '';
+
+            $('#modalDel').modal('hide');
+
             $scope.fetchThings();
         }, function (result) {
             console.error('error:', result);
+            $scope.delFormData.busy = false;
         });
     };