浏览代码

convert dynamic properties in-place

Falko Schindler 1 年之前
父节点
当前提交
cde6c330b6
共有 2 个文件被更改,包括 7 次插入27 次删除
  1. 1 1
      nicegui/elements/aggrid.js
  2. 6 26
      nicegui/static/utils/dynamic_properties.js

+ 1 - 1
nicegui/elements/aggrid.js

@@ -17,7 +17,7 @@ export default {
           this.gridOptions.columnDefs[column].cellRenderer = (params) => (params.value ? params.value : "");
         }
       }
-      this.gridOptions = convertDynamicProperties(this.gridOptions);
+      convertDynamicProperties(this.gridOptions);
 
       // Code for CheckboxRenderer https://blog.ag-grid.com/binding-boolean-values-to-checkboxes-in-ag-grid/
       function CheckboxRenderer() {}

+ 6 - 26
nicegui/static/utils/dynamic_properties.js

@@ -1,41 +1,21 @@
-export function hasDynamicProperties(obj) {
-  if (typeof obj !== "object" || obj === null) return false;
-  if (Array.isArray(obj)) {
-    return obj.some((v) => hasDynamicProperties(v));
-  }
-  for (const [key, value] of Object.entries(obj)) {
-    if (key.startsWith(":")) {
-      return true;
-    }
-    if (hasDynamicProperties(value)) {
-      return true;
-    }
-  }
-  return false;
-}
-
 export function convertDynamicProperties(obj) {
-  if (!hasDynamicProperties(obj)) {
-    // double-loop hierarchy is probably safer and uses less RAM if dynamic not used
-    return obj;
-  }
   if (typeof obj !== "object" || obj === null) {
-    return obj;
+    return;
   }
   if (Array.isArray(obj)) {
-    return obj.map((v) => convertDynamicProperties(v));
+    obj.forEach((v) => convertDynamicProperties(v));
+    return;
   }
-  const targetObj = {};
   for (const [attr, value] of Object.entries(obj)) {
     if (attr.startsWith(":")) {
       try {
-        targetObj[attr.slice(1)] = new Function("return " + value)();
+        obj[attr.slice(1)] = new Function("return " + value)();
+        delete obj[attr];
       } catch (e) {
         console.error(`Error while converting ${attr} attribute to function:`, e);
       }
     } else {
-      targetObj[attr] = convertDynamicProperties(value);
+      convertDynamicProperties(value);
     }
   }
-  return targetObj;
 }