Newbie needs constructor that takes an object with named properties

I’m writing an extension where I’ve created a class that extends St.BoxLayout, to act as a panel in the extension’s dialog. I want to instantiate it from extension.js by calling it’s constructor and pass in information. Passing a simple value parameter produces the error:
Argument to the constructor of StatusPanel should be an object with properties to set

I expect I’ll need to call it like this:
const panel = new StatusPanel({param1: value});

What should the constructor look like? I’ve tried:

constructor(obj){
  this.param1 = obj['param1'];
}

…but it produces the same error. Any help appreciated.

Your expectation of how to construct StatusPanel looks correct to me.

If the properties are registered as GObject properties (with GObject.ParamSpec), you can just omit the constructor and the _init() method on your class, and it will do the right thing.

If they are not registered as GObject properties (just regular JS properties), you can define an _init() method that removes the non-GObject properties before chaining up:

_init({ param1 = someDefaultValue, ...otherProps } = {}) {
  super._init(otherProps);
  this.param1 = param1;
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.