Quellcode durchsuchen

refactoring unsavedChangesSrv and who now owns original

Torkel Ödegaard vor 11 Jahren
Ursprung
Commit
8ca06e11de

+ 1 - 1
src/app/controllers/dashLoader.js

@@ -77,7 +77,7 @@ function (angular, _, moment) {
             $scope.share = dashboard.share_link(dashboard.current.title,'temp',result._id);
           }
 
-          $rootScope.$emit('dashboard-saved');
+          $rootScope.$emit('dashboard-saved', dashboard.current);
         });
     };
 

+ 0 - 6
src/app/services/dashboard.js

@@ -62,7 +62,6 @@ function (angular, $, kbn, _, config, moment, Modernizr) {
     $rootScope.$on('$routeChangeSuccess',function(){
       // Clear the current dashboard to prevent reloading
       self.current = {};
-      self.original = null;
       self.indices = [];
       route();
     });
@@ -169,10 +168,6 @@ function (angular, $, kbn, _, config, moment, Modernizr) {
 
       $rootScope.$emit('dashboard-loaded', self.current);
 
-      $timeout(function() {
-        self.original = angular.copy(self.current);
-      }, 1000);
-
       return true;
     };
 
@@ -385,7 +380,6 @@ function (angular, $, kbn, _, config, moment, Modernizr) {
           if(type === 'dashboard') {
             $location.path('/dashboard/elasticsearch/'+title);
           }
-          self.original = angular.copy(self.current);
           return result;
         },
         // Failure

+ 15 - 3
src/app/services/unsavedChangesSrv.js

@@ -16,6 +16,18 @@ function (angular, _, config) {
     var self = this;
     var modalScope = $rootScope.$new();
 
+    $rootScope.$on("dashboard-loaded", function(event, newDashboard ) {
+      self.original = angular.copy(newDashboard);
+    });
+
+    $rootScope.$on("dashboard-saved", function(event, savedDashboard) {
+      self.original = angular.copy(savedDashboard);
+    });
+
+    $rootScope.$on("$routeChangeSuccess", function() {
+      self.original = null;
+    });
+
     window.onbeforeunload = function () {
       if (self.has_unsaved_changes()) {
         return "There are unsaved changes to this dashboard";
@@ -47,12 +59,12 @@ function (angular, _, config) {
     };
 
     this.has_unsaved_changes = function () {
-      if (!dashboard.original) {
+      if (!self.original) {
         return false;
       }
 
       var current = angular.copy(dashboard.current);
-      var original = dashboard.original;
+      var original = self.original;
 
       // ignore timespan changes
       current.services.filter.time = original.services.filter.time = {};
@@ -83,7 +95,7 @@ function (angular, _, config) {
     };
 
     modalScope.ignore = function() {
-      dashboard.original = null;
+      self.original = null;
       self.goto_next();
     };