Use new view model calling helper where possible

This commit is contained in:
Gina Häußge 2015-08-19 11:48:22 +02:00
parent f7117900e6
commit f4a952ebe6
8 changed files with 57 additions and 148 deletions

View file

@ -88,8 +88,6 @@ $(function() {
self.settingsViewModel = parameters[0];
self.onWizardFinish = function() {
if (self.unbound) return;
self.settingsViewModel.enqueueForSaving({
webcam: {
streamUrl: self.settingsViewModel.webcam_streamUrl(),

View file

@ -46,17 +46,12 @@ function DataUpdater(allViewModels) {
// Only consider it a real disconnect if the trial number has exceeded our threshold.
var handled = false;
_.each(self.allViewModels, function(viewModel) {
if (handled == true) {
return;
}
if (viewModel.hasOwnProperty("onServerDisconnect")) {
if (!viewModel.onServerDisconnect()) {
handled = true;
}
}
});
callViewModelsIf(
self.allViewModels,
"onServerDisconnect",
function() { return !handled; },
function(method) { handled = !method() || handled; }
);
if (handled) {
return;
@ -81,17 +76,12 @@ function DataUpdater(allViewModels) {
self._onreconnectfailed = function() {
var handled = false;
_.each(self.allViewModels, function(viewModel) {
if (handled == true) {
return;
}
if (viewModel.hasOwnProperty("onServerDisconnect")) {
if (!viewModel.onServerDisconnect()) {
handled = true;
}
}
});
callViewModelsIf(
self.allViewModels,
"onServerDisconnect",
function() { return !handled; },
function(method) { handled = !method() || handled; }
);
if (handled) {
return;
@ -133,11 +123,7 @@ function DataUpdater(allViewModels) {
if ($("#offline_overlay").is(":visible")) {
hideOfflineOverlay();
_.each(self.allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("onDataUpdaterReconnect")) {
viewModel.onDataUpdaterReconnect();
}
});
callViewModels(self.allViewModels, "onDataUpdaterReconnect");
if ($('#tabs li[class="active"] a').attr("href") == "#control") {
$("#webcam_image").attr("src", CONFIG_WEBCAM_STREAM + "?" + new Date().getTime());
@ -154,29 +140,22 @@ function DataUpdater(allViewModels) {
break;
}
case "history": {
_.each(self.allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("fromHistoryData")) {
viewModel.fromHistoryData(data);
}
});
callViewModels(self.allViewModels, "fromHistoryData", [data]);
break;
}
case "current": {
_.each(self.allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("fromCurrentData")) {
viewModel.fromCurrentData(data);
}
});
callViewModels(self.allViewModels, "fromCurrentData", [data]);
break;
}
case "slicingProgress": {
gcodeUploadProgressBar.text(_.sprintf(gettext("Slicing ... (%(percentage)d%%)"), {percentage: Math.round(data["progress"])}));
_.each(self.allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("onSlicingProgress")) {
viewModel.onSlicingProgress(data["slicer"], data["model_path"], data["machinecode_path"], data["progress"]);
}
});
callViewModels(self.allViewModels, "onSlicingProgress", [
data["slicer"],
data["model_path"],
data["machinecode_path"],
data["progress"]
]);
break;
}
case "event": {
@ -277,19 +256,12 @@ function DataUpdater(allViewModels) {
break;
}
case "timelapse": {
_.each(self.allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("fromTimelapseData")) {
viewModel.fromTimelapseData(data);
}
});
callViewModels(self.allViewModels, "fromTimelapseData", [data]);
break;
}
case "plugin": {
_.each(self.allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("onDataUpdaterPluginMessage")) {
viewModel.onDataUpdaterPluginMessage(data.plugin, data.data);
}
})
callViewModels(self.allViewModels, "onDataUpdaterPluginMessage", [data.plugin, data.data]);
break;
}
}
}

View file

@ -366,23 +366,13 @@ $(function() {
tabs.on('show', function (e) {
var current = e.target.hash;
var previous = e.relatedTarget.hash;
_.each(allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("onTabChange")) {
viewModel.onTabChange(current, previous);
}
});
callViewModels(allViewModels, "onTabChange", [current, previous]);
});
tabs.on('shown', function (e) {
var current = e.target.hash;
var previous = e.relatedTarget.hash;
_.each(allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("onAfterTabChange")) {
viewModel.onAfterTabChange(current, previous);
}
});
callViewModels(allViewModels, "onAfterTabChange", [current, previous]);
});
// Fix input element click problems on dropdowns
@ -397,11 +387,7 @@ $(function() {
//~~ Starting up the app
_.each(allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("onStartup")) {
viewModel.onStartup();
}
});
callViewModels(allViewModels, "onStartup");
//~~ view model binding
@ -432,6 +418,8 @@ $(function() {
targets = [targets];
}
viewModel._bindings = [];
_.each(targets, function(target) {
if (target == undefined) {
return;
@ -446,44 +434,41 @@ $(function() {
if (object == undefined || !object.length) {
log.info("Did not bind view model", viewModel.constructor.name, "to target", target, "since it does not exist");
viewModel.unbound = true;
return;
}
var element = object.get(0);
if (element == undefined) {
log.info("Did not bind view model", viewModel.constructor.name, "to target", target, "since it does not exist");
viewModel.unbound = true;
return;
}
try {
ko.applyBindings(viewModel, element);
viewModel._bindings.push(target);
if (viewModel.hasOwnProperty("onBoundTo")) {
viewModel.onBoundTo(target, element);
}
log.debug("View model", viewModel.constructor.name, "bound to", target);
} catch (exc) {
log.error("Could not bind view model", viewModel.constructor.name, "to target", target, ":", (exc.stack || exc));
viewModel.unbound = true;
}
});
}
viewModel._unbound = viewModel._bindings != undefined && viewModel._bindings.length == 0;
if (viewModel.hasOwnProperty("onAfterBinding")) {
viewModel.onAfterBinding();
}
});
_.each(allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("onAllBound")) {
viewModel.onAllBound(allViewModels);
}
});
callViewModels(allViewModels, "onAllBound", [allViewModels]);
log.info("... binding done");
_.each(allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("onStartupComplete")) {
viewModel.onStartupComplete();
}
});
callViewModels(allViewModels, "onStartupComplete");
};
if (!_.has(viewModelMap, "settingsViewModel")) {

View file

@ -448,10 +448,8 @@ $(function() {
self.onAllBound = function (allViewModels) {
var additionalControls = [];
_.each(allViewModels, function (viewModel) {
if (viewModel.hasOwnProperty("getAdditionalControls")) {
additionalControls = additionalControls.concat(viewModel.getAdditionalControls());
}
callViewModels(allViewModels, "getAdditionalControls", function(method) {
additionalControls = additionalControls.concat(method());
});
if (additionalControls.length > 0) {
self.additionalControls = additionalControls;

View file

@ -53,11 +53,7 @@ $(function() {
self.currentUser(response);
_.each(self.allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("onUserLoggedIn")) {
viewModel.onUserLoggedIn(response);
}
});
callViewModels(self.allViewModels, "onUserLoggedIn", [response]);
} else {
self.loggedIn(false);
self.username(undefined);
@ -66,11 +62,7 @@ $(function() {
self.currentUser(undefined);
_.each(self.allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("onUserLoggedOut")) {
viewModel.onUserLoggedOut();
}
});
callViewModels(self.allViewModels, "onUserLoggedOut");
}
};
@ -138,4 +130,4 @@ $(function() {
[],
[]
]);
});
});

View file

@ -344,28 +344,16 @@ $(function() {
self.settingsDialog.on('show', function(event) {
if (event.target.id == "settings_dialog") {
self.requestTranslationData();
_.each(allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("onSettingsShown")) {
viewModel.onSettingsShown();
}
});
callViewModels(allViewModels, "onSettingsShown");
}
});
self.settingsDialog.on('hidden', function(event) {
if (event.target.id == "settings_dialog") {
_.each(allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("onSettingsHidden")) {
viewModel.onSettingsHidden();
}
});
callViewModels(allViewModels, "onSettingsHidden");
}
});
self.settingsDialog.on('beforeSave', function () {
_.each(allViewModels, function (viewModel) {
if (viewModel.hasOwnProperty("onSettingsBeforeSave")) {
viewModel.onSettingsBeforeSave();
}
});
callViewModels(allViewModels, "onSettingsBeforeSave");
});
$(".reload_all", self.settingsUpdatedDialog).click(function(e) {

View file

@ -90,18 +90,10 @@ $(function() {
self.onAllBound = function(allViewModels) {
self.userSettingsDialog.on('show', function() {
_.each(allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("onUserSettingsShown")) {
viewModel.onUserSettingsShown();
}
});
callViewModels(allViewModels, "onUserSettingsShown");
});
self.userSettingsDialog.on('hidden', function() {
_.each(allViewModels, function(viewModel) {
if (viewModel.hasOwnProperty("onUserSettingsHidden")) {
viewModel.onUserSettingsHidden();
}
});
callViewModels(allViewModels, "onUserSettingsHidden");
});
}
@ -112,4 +104,4 @@ $(function() {
["loginStateViewModel", "usersViewModel"],
["#usersettings_dialog"]
]);
});
});

View file

@ -19,11 +19,7 @@ $(function() {
if (!CONFIG_WIZARD || !(CONFIG_FIRST_RUN || self.loginState.isAdmin())) return;
self.getWizardDetails(function(response) {
_.each(self.allViewModels, function(viewModel) {
if (!viewModel.unbound && viewModel.hasOwnProperty("onWizardDetails")) {
viewModel.onWizardDetails(response);
}
});
callViewModels(self.allViewModels, "onWizardDetails", [response]);
if (!self.isDialogActive()) {
self.wizardDialog.modal({
@ -76,11 +72,7 @@ $(function() {
var active = tab[0].id;
if (active != undefined) {
_.each(allViewModels, function(viewModel) {
if (!viewModel.unbound && viewModel.hasOwnProperty("onAfterWizardTabChange")) {
viewModel.onAfterWizardTabChange(active);
}
});
callViewModels(allViewModels, "onAfterWizardTabChange", [active]);
}
},
onTabChange: function(tab, navigation, index, nextTabIndex, nextTab) {
@ -99,28 +91,20 @@ $(function() {
if (current != undefined && next != undefined) {
var result = true;
_.each(allViewModels, function(viewModel) {
if (!viewModel.unbound && viewModel.hasOwnProperty("onWizardTabChange")) {
result = result && (viewModel.onWizardTabChange(current, next) !== false);
}
callViewModels(allViewModels, "onWizardTabChange", function(method) {
result = result && (method(current, next) !== false);
});
return result;
}
},
onFinish: function(tab, navigation, index) {
var closeDialog = true;
_.each(allViewModels, function(viewModel) {
if (!viewModel.unbound && viewModel.hasOwnProperty("onBeforeWizardFinish")) {
closeDialog = closeDialog && (viewModel.onBeforeWizardFinish() !== false);
}
callViewModels(allViewModels, "onBeforeWizardFinish", function(method) {
closeDialog = closeDialog && (method() !== false);
});
if (closeDialog) {
_.each(allViewModels, function(viewModel) {
if (!viewModel.unbound && viewModel.hasOwnProperty("onWizardFinish")) {
viewModel.onWizardFinish();
}
});
callViewModels(allViewModels, "onWizardFinish");
self.finishWizard(function() {
self.closeDialog();
});