I have been reading about bind_with_mapping method in JavaScript.
I want to persist preference settings like so:
this.settings = Gio.Settings.new("<app_id>");
this.settings.bind_with_mapping(
"settings",
this._comboRow,
"selected",
Gio.SettingsBindFlags.DEFAULT,
(arg1, settingsVariant) => {
const { model } = this._comboRow;
for (let i = 0; i < model.get_n_items(); i++) {
const stringObj = model.get_item(i);
if (stringObj?.string === settingsVariant.unpack()) {
this._comboRow.selected = i;
return true;
}
}
return true;
},
(selected, selectedVariantType) => {
const stringObj = this._comboRow.model.get_item(selected);
if (stringObj?.string) {
return new GLib.Variant("s", stringObj.string);
}
return new GLib.Variant("s", 'HEX');
}
);
When I select an item in the comboRow, the set_mapping
callback(the second callback in the above example) is invoked and updates the settings correctly.
However, when I change the settings, I want the corresponding item in the ComboRow to be selected. The get_mapping
callback(the first callback in the above example) is invoked with two arguments. The first argument is 0
(I’m not even sure what it is). And the second argument is a GLib.Variant holding the settings. I can unpack and get the current settings.
I’m not sure how I can use that information passed as arguments to the get_mapping
function to update the comboRow. You will notice I tried to update the comboRow’s selected
property in the callback but it doesn’t work.
How does the get_mapping
function work in JavaScript? I’ve read the original docs here and the corresponding descriptions for the callbacks:
The set_mapping
function seems clear and straight forward but the get_mapping
function is kinda confusing to me.