From 68431770d8e30a5c2a460290adff856384992e5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 17 Aug 2015 16:46:40 +0200 Subject: [PATCH] Allow view models to prevent settings refresh --- .../static/js/app/viewmodels/settings.js | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/octoprint/static/js/app/viewmodels/settings.js b/src/octoprint/static/js/app/viewmodels/settings.js index 3e14f625..51fcc79a 100644 --- a/src/octoprint/static/js/app/viewmodels/settings.js +++ b/src/octoprint/static/js/app/viewmodels/settings.js @@ -6,6 +6,8 @@ $(function() { self.users = parameters[1]; self.printerProfiles = parameters[2]; + self.allViewModels = []; + self.receiving = ko.observable(false); self.sending = ko.observable(false); self.callbacks = []; @@ -228,6 +230,8 @@ $(function() { }; self.onAllBound = function(allViewModels) { + self.allViewModels = allViewModels; + self.settingsDialog.on('show', function(event) { if (event.target.id == "settings_dialog") { self.requestTranslationData(); @@ -403,7 +407,7 @@ $(function() { /** * Fetches the settings as currently stored in this client instance. */ - self._getLocalData = function() { + self.getLocalData = function() { var data = {}; if (self.settings != undefined) { data = ko.mapping.toJS(self.settings); @@ -474,7 +478,7 @@ $(function() { if (local) { // local is true, so we'll keep all local changes and only update what's been updated server side serverChangedData = getOnlyChangedData(response, self.lastReceivedSettings); - clientChangedData = getOnlyChangedData(self._getLocalData(), self.lastReceivedSettings); + clientChangedData = getOnlyChangedData(self.getLocalData(), self.lastReceivedSettings); } else { // local is false or unset, so we'll forcefully update with the settings from the server serverChangedData = response; @@ -554,7 +558,7 @@ $(function() { self.sending(true); // we also only send data that actually changed when no data is specified - data = getOnlyChangedData(self._getLocalData(), self.lastReceivedSettings); + data = getOnlyChangedData(self.getLocalData(), self.lastReceivedSettings); } $.ajax({ @@ -580,13 +584,31 @@ $(function() { }; self.onEventSettingsUpdated = function() { + var preventSettingsRefresh = _.any(self.allViewModels, function(viewModel) { + if (viewModel.hasOwnProperty("onSettingsPreventRefresh")) { + try { + return viewModel["onSettingsPreventRefresh"](); + } catch (e) { + log.warn("Error while calling onSettingsPreventRefresh on", viewModel, ":", e); + return false; + } + } else { + return false; + } + }); + + if (preventSettingsRefresh) { + // if any of our viewmodels prevented this refresh, we'll just return now + return; + } + if (self.isDialogActive()) { // dialog is open and not currently busy... if (self.sending() || self.receiving()) { return; } - if (!hasDataChanged(self._getLocalData(), self.lastReceivedSettings)) { + if (!hasDataChanged(self.getLocalData(), self.lastReceivedSettings)) { // we don't have local changes, so just fetch new data self.requestData(); } else {