Allow to define additional bindings for existing view models

Adding a line such as

    OCTOPRINT_ADDITIONAL_BINDINGS.push(["gcodeFilesViewModel", "#tab_plugin_my_other_file_plugin"]);

to a plugin JS now allows the plugin to configure existing view models to bind to additional elements, not just the ones configured by the view model itself. This allows binding view models of standard components to other component types and thus "moving" components, e.g. the files dialog from the sidebar to the tab section.
This commit is contained in:
Gina Häußge 2015-03-09 13:11:43 +01:00
parent a87c3f6d4d
commit 1a96480aa7
2 changed files with 27 additions and 2 deletions

View file

@ -88,6 +88,22 @@ $(function() {
return new viewModelClass(constructorParameters);
};
// map any additional view model bindings we might need to make
var additionalBindings = {};
_.each(OCTOPRINT_ADDITIONAL_BINDINGS, function(bindings) {
var viewModelId = bindings[0];
var viewModelBindTargets = bindings[1];
if (!_.isArray(viewModelBindTargets)) {
viewModelBindTargets = [viewModelBindTargets];
}
if (!additionalBindings.hasOwnProperty(viewModelId)) {
additionalBindings[viewModelId] = viewModelBindTargets;
} else {
additionalBindings[viewModelId] = additionalBindings[viewModelId].concat(viewModelBindTargets);
}
});
// helper for translating the name of a view model class into an identifier for the view model map
var _getViewModelId = function(viewModel){
var name = viewModel[0].name;
@ -129,8 +145,16 @@ $(function() {
}
// we could resolve the depdendencies and the view model is not defined yet => add it, it's now fully processed
var viewModelBindTarget = viewModel[2];
allViewModelData.push([viewModelInstance, viewModelBindTarget]);
var viewModelBindTargets = viewModel[2];
if (!_.isArray(viewModelBindTargets)) {
viewModelBindTargets = [viewModelBindTargets];
}
if (additionalBindings.hasOwnProperty(viewModelId)) {
viewModelBindTargets = viewModelBindTargets.concat(additionalBindings[viewModelId]);
}
allViewModelData.push([viewModelInstance, viewModelBindTargets]);
allViewModels.push(viewModelInstance);
viewModelMap[viewModelId] = viewModelInstance;
}

View file

@ -28,4 +28,5 @@
var OCTOPRINT_VIEWMODELS = [];
var ADDITIONAL_VIEWMODELS = [];
var OCTOPRINT_ADDITIONAL_BINDINGS = [];
</script>