When implementing frontend components, you’ll sooner or later want to define your own KnockoutJS viewmodels in order to provide custom functionality.

Registering custom viewmodels

Register your viewmodel with OctoPrint’s web app by pushing a 3-tuple consisting of your viewmodel’s class, a list of all required dependencies to be injected into the constructor and a list of all elements to bind the viewmodel to.


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

        self.loginState = parameters[0]; // requested as first constructor parameter below

        // more of your viewmodel's implementation

        ["#some_div", "#some_other_div"]


Each provided binding target may be either a string which will then be passed to jQuery’s $(...) method to resolve the target, or alternatively directly a jQuery element.


OctoPrint will try to inject all viewmodel dependencies requested by your viewmodel. In order to do this it will perform multiple passes iterating over all registered viewmodels and collecting the necessary depedencies prior to construction. Circular depedencies (A dependens on B, B on C, C on A) naturally cannot be resolved and will cause an error to be logged to the Javascript console.

OctoPrint’s core currently comes with the following viewmodels that your plugin can request for injection:

Viewmodel that holds the appearance settings (name, color and transparency flag).
Viewmodel for the connection sidebar entry.
Viewmodel for the control tab.
Viewmodel for the files sidebar entry.
Viewmodel for the first run dialog.
Viewmodel for the gcode viewer tab.
Viewmodel for the logfile settings dialog.
Viewmodel for the current loginstate of the user, very interesting for plugins that need to evaluate the current login state or information about the current user, e.g. associated roles.
Viewmodel for the navigation bar.
Viewmodel for the printer profiles settings dialog.
Viewmodel for the current printer state, very intersting for plugins that need to know information about the current print job, if the printer is connected, operational etc.
Viewmodel for the settings dialog, also holds all settings to be used by other viewmodels, hence very interesting for plugins as well.
Viewmodel for the slicing dialog.
Viewmodel for the temperature tab, also holds current temperature information which might be interesting for plugins.
Viewmodel for the terminal tab, also holds terminal log entries.
Viewmodel for the timelapse tab.
Viewmodel for the user management in the settings dialog.
Viewmodel for settings associated with the currently logged in user, used for the user settings dialog.

Additionally each plugin’s viewmodel will be added to the viewmodel map used for resolving dependencies as well, using the viewmodel’s class name with a lower case first character as identifier (so “MyCustomViewModel” will be registered for dependency injection as “myCustomViewModel”).


OctoPrint’s web application will call several callbacks on all registered viewmodels, provided they implement them. Those are listed below:

Called when the first initialization has been done: All viewmodels are constructed and hence their dependencies resolved, no bindings have been done yet.
Called per viewmodel before attempting to bind it to its binding targets.
Called per viewmodel after binding it to its binding targets.
Called after all viewmodels have been bound, with the list of all viewmodels as the single parameter.
Called after the startup of the web app has been completed.
Called if a disconnect from the server is detected.
Called when the connection to the server has been reestablished after a disconnect.
Called when history data is received from the server. Usually that happens only after initial connect in order to transmit the temperature and terminal log history to the connecting client. Called with the data as single parameter.
Called when current printer status data is received from the server with the data as single parameter.
onSlicingProgress(slicer, modelPath, machineCodePath, progress)
Called on slicing progress, call rate is once per percentage point of the progress at maximum.
Called on firing of an event of type EventName, e.g. onEventPrintDone. See the list of available events for the possible events and their payloads.
Called when timelapse configuration data is received from the server. Usually that happens after initial connect.
onDataUpdaterPluginMessage(plugin, message)
Called when a plugin message is pushed from the server with the identifier of the calling plugin as first and the actual message as the second parameter. Note that the latter might be a full fledged object, depending on the plugin sending the message. You can use this method to asynchronously push data from your plugin’s server component to it’s frontend component.
Called when a user gets logged into the web app, either passively (upon initial load of the page due to a valid “Remember Me” cookie) or due to an active completion of the login dialog. The user data of the just logged in user will be provided as only parameter.
Called when a user gets logged out of the web app.
onTabChange(current, previous)
Called before the main tab view switches to a new tab, so before the new tab becomes visible. Called with the current and previous tab’s hash (e.g. #control).
Called after the main tab view switches to a new tab, so after the new tab becomes visible. Called with the current and previous tab’s hash (e.g. #control).
Your viewmodel may return additional custom control definitions for inclusion on the “Control” tab of OctoPrint’s interface. See the custom control feature.
Called when the settings dialog is shown.
Called when the settings dialog is hidden.
Called just before the settings viewmodel is sent to the server. This is useful, for example, if your plugin needs to compute persisted settings from a custom viewmodel.
Called when the user settings dialog is shown.
Called when the user settings dialog is hidden.

In order to hook into any of those callbacks, just have your viewmodel define a function named accordingly, e.g. to get called after all viewmodels have been bound during application startup, implement a function onAllBound on your viewmodel, taking a list of all bound viewmodels:

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

        // ...

        self.onAllBound = function(allViewModels) {
            // do something with them

        // ...

        ["#some_div", "#some_other_div"]

See also

OctoPrint’s core viewmodels
OctoPrint’s own viewmodels use the same mechanisms for interacting with each other and the web application as plugins. Their sourcecode is therefore a good point of reference on how to achieve certain things.
KnockoutJS documentation
OctoPrint makes heavy use of KnockoutJS for building up its web app.