Binding button in setting to a function?


I try to bind a function to a button like other plugins does in its settings. But I get an error that binding failed: Could not bind view model SettingsViewModel to target #settings_dialog

The interesting thing is if I changed the binding to e.g. click: settings.plugins.SlicerEstimator.BlaBla there is no error any more although the function does not exists. But sit in front of it for hours... :rage:

What I missed and why is there not error binding to a function not exists?


For that, you will need to use custom bindings, so you will have to bind a viewmodel, with a dependency on the settingsViewModel and update every binding to reference that rather than the internal settings. It's a bit complicated to type out for me now but if that doesn't make sense I can write a better explanation later.

Hi hate works now with a setting I think I tried before...

I moved the function to the ViewModel as my variables and it runs... that was the setup I started with...

But why can e.g. themify create the function in the custom ViewModel which does not work in my case. Compared themify function should be created in self.setting.addNewIcon...

That's what I mentioned with the 'Custom Bindings' settings.

In your template configuration somewhere (in Python) you have set custom_bindings=False. What this means is that there will be no special bindings for your plugins templates of your viewmodel.

custom_bindings=True, the default, will mean that the settingsViewModel is not bound to your plugin's templates. Instead, whatever viewmodel you bind to the element (with the target field I think, in the JS) will become the 'root'. From there you will have to change all your existing bindings from the ones that work as if they are in the settings viewmodel to being settingsViewModel.... at the start instead, as your viewmodel depends on it.

the other option is doing like you did and you're actually attaching your function to the settingsViewModel, which in turn allows the binding to work with custom_bindings=False because the function is now part of's all about scope.

I would generally recommend against doing that though - it has high potential to conflict with other plugins or core OctoPrint at some point, it is more resilient to do it in your own viewmodel.

Couldn't you use self.settings = parameters[0].settings and then not have to rewrite the previous bindings?

So switch custom binding on as I can use the scope of my own ViewModel that there could not be a conflict?

That would be correct. I still wonder if you could use self.settings = parameters[0].settings with custom_bindings=True and not have to refactor all your current bindings. Otherwise it's a fairly easy find/replace.