[pve-devel] [PATCH manager 2/7] ui: VMSelector: improve {set, get}Value handling with a loading store

Dominik Csapak d.csapak at proxmox.com
Mon Mar 6 15:23:30 CET 2023


when we do {set,get}Value during a store load, the store might be empty
or incomplete, so defer the selection after the load and cache the value
for getValue invocations until the store is loaded

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 www/manager6/form/VMSelector.js | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

diff --git a/www/manager6/form/VMSelector.js b/www/manager6/form/VMSelector.js
index d80a21ec0..bb670f930 100644
--- a/www/manager6/form/VMSelector.js
+++ b/www/manager6/form/VMSelector.js
@@ -117,6 +117,9 @@ Ext.define('PVE.form.VMSelector', {
 
     getValue: function() {
 	var me = this;
+	if (me.savedValue !== undefined) {
+	    return me.savedValue;
+	}
 	var sm = me.getSelectionModel();
 	var selection = sm.getSelection();
 	var values = [];
@@ -130,6 +133,20 @@ Ext.define('PVE.form.VMSelector', {
 	return values;
     },
 
+    setValueSelection: function(value) {
+	let me = this;
+
+	let store = me.getStore();
+	let selection = value.map(item => store.findRecord('vmid', item, 0, false, true, true)).filter(r => r);
+
+	let sm = me.getSelectionModel();
+	if (selection.length) {
+	    sm.select(selection);
+	} else {
+	    sm.deselectAll();
+	}
+    },
+
     setValue: function(value) {
 	let me = this;
 	if (!Ext.isArray(value)) {
@@ -137,10 +154,15 @@ Ext.define('PVE.form.VMSelector', {
 	}
 
 	let store = me.getStore();
-	let selection = value.map(item => store.findRecord('vmid', item, 0, false, true, true)).filter(r => r);
-
-	me.getSelectionModel().select(selection);
-
+	if (!store.isLoaded()) {
+	    me.savedValue = value;
+	    store.on('load', function() {
+		me.setValueSelection(value);
+		delete me.savedValue;
+	    }, { single: true });
+	} else {
+	    me.setValueSelection(value);
+	}
 	return me.mixins.field.setValue.call(me, value);
     },
 
-- 
2.30.2






More information about the pve-devel mailing list