Settings button isn´t working - Need help with Knockout

Hi,

I´m struggling with Knockout and some user elements in my OctoPrint plugin. I want to add a user button to the settings menu entry of my plugin, but I don´t get any debug output in my browser when I click the button:

SpoolScale_settings.jinja2

	<div class="control-group">
		<div class="controls">
			<button class="btn" type="button" data-bind="click: Test">Test</button>
		</div>
	</div>

SpoolScale.js

$(function()
{
    function SpoolscaleViewModel(parameters)
	{
        var self = this;

		self.Weight = ko.observable();

        self.onDataUpdaterPluginMessage = function(plugin, data)
		{
            if((plugin != "SpoolScale") || (!data))
			{
                return;
            }

			console.log("Update");
	
			if(data.Weight)
			{
				self.Weight("Weight: " + data.Weight);
			}
        };

		self.onBeforeBinding = function()
		{
			console.log("Before binding");
        }

		self.Test = function()
		{
			console.log("Test");
		};
    }

    OCTOPRINT_VIEWMODELS.push({
        construct: SpoolscaleViewModel,
        dependencies: [ "settingsViewModel" ],
        elements: [ "#navbar_plugin_SpoolScale", "#settings_plugin_SpoolScale" ]
    });
});

My variable "Weight" from my navbar gets updated all the time (the value is delivered by init.py).

SpoolScale_navbar.jinja2

<div class="navbar-text">
    <span id="weight" data-bind="text: Weight"></span> g
</div>

init.py

class SpoolscalePlugin(octoprint.plugin.StartupPlugin,
                        octoprint.plugin.AssetPlugin,
                        octoprint.plugin.TemplatePlugin,
                        octoprint.plugin.SettingsPlugin):

	def __init__(self):
		self.__Settings = dict()
		self.__Settings["DOUT"] = 17
		self.__Settings["SCK"] = 4
		self.__Settings["Weight"] = 4.32
		self.__HX = HX711.HX711(DOUT = int(self.__Settings["DOUT"]), SCK = int(self.__Settings["SCK"]))

	def __StartTimer(self):
		self.__UpdateTimer = RepeatedTimer(1, self.on_Timer, run_first = True)
		self.__UpdateTimer.start()

	def on_Timer(self):
		self._plugin_manager.send_plugin_message(self._identifier,
												dict(Weight = int(random() * 100))
												)

	def on_after_startup(self):
		self._logger.info("Version: {}")
		self.__StartTimer()

	def on_shutdown(self):
		if(self.__HX):
			del self.__HX

	def on_settings_save(self, data):
		self.__HX = HX711.HX711(DOUT = int(self.__Settings["DOUT"]), SCK = int(self.__Settings["SCK"]))

	def get_template_configs(self):
		return [
			dict(type = "navbar", custom_bindings = False),
			dict(type = "settings", custom_bindings = False)
		]

	def get_settings_defaults(self):
		return self.__Settings

	def get_assets(self):
		return {
			"js": ["js/SpoolScale.js"],
			"css": ["css/SpoolScale.css"],
			"less": ["less/SpoolScale.less"]
		}

	def get_update_information(self):
		return {
			"SpoolScale": {
				"displayName": "Spoolscale Plugin",
				"displayVersion": self._plugin_version,

				# version check: github repository
				"type": "github_release",
				"user": "kampi",
				"repo": "OctoPrint-Spoolscale",
				"current": self._plugin_version,

				# update method: pip
				"pip": "https://github.com/kampi/OctoPrint-Spoolscale/archive/{target_version}.zip",
			}
		}

__plugin_name__ = "Spool Scale"
__plugin_pythoncompat__ = ">3,<4"

def __plugin_load__():
	global __plugin_implementation__
	__plugin_implementation__ = SpoolscalePlugin()

	global __plugin_hooks__
	__plugin_hooks__ = {
		"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information
	}

What is the reason why the update of the navbar is working as expected, but the button from the settings menu isn´t working? I don´t get it...

Took the liberty and moved it to Development

1 Like

You have told OctoPrint you don't have custom bindings, then you have tried to use a custom binding. Try switching these to True. Also, these errors should be shown in the console in your browser so check there first whenever something doesn't work

Chrome I believe gives more details error messages as it pertains to knockout IIRC.

1 Like