update +++

This commit is contained in:
lb 2023-04-03 11:35:14 +08:00
parent 13753f0c6c
commit 2afdb1d23d
2 changed files with 80 additions and 26 deletions

View File

@ -252,34 +252,87 @@ export default {
};
},
mounted() {
/** 临时保存所有发出的请求 */
const promiseHistory = {};
/** 处理 injectTo 选项 */
this.configs.form.rows.forEach((row) => {
row.forEach((col) => {
if ("injectTo" in col && Array.isArray(col.injectTo)) {
const valueProp = "optionValue" in col ? col.optionValue : "id";
const unwatch = this.$watch(
() => this.dataForm[col.prop],
(newVal) => {
const chosenObject = this.cachedList[col.prop].find((i) => i[valueProp] === newVal);
if (chosenObject) {
//
col.injectTo.map((item) => {
console.log("[setting ---> ]:", item[1], "to", `dataForm[${item[0]}]`);
this.$set(this.dataForm, item[0], chosenObject[item[1]]);
this.$forceUpdate();
});
} else {
console.log("DialogJustForm mounted(): 没有找到 injectTo 关联的对象");
if (col.fetchData && typeof col.fetchData === "function" && !col.hasPrev) {
// -
promiseHistory[col.prop] = col.fetchData().then(({ data: res }) => {
if (res.code === 0) {
if ("list" in res.data) {
// options
this.$set(
col,
"options",
res.data.list.map((i) => ({
label: col.optionLabel ? i[col.optionLabel] : i.name,
value: col.optionValue ? i[col.optionValue] : i.id,
}))
);
//
if ("injectTo" in col) {
//
this.cachedList[col.prop] = res.data.list;
//
const valueProp = "optionValue" in col ? col.optionValue : "id";
const unwatch = this.$watch(
() => this.dataForm[col.prop],
(val) => {
const chosenObject = this.cachedList[col.prop].find((i) => i[valueProp] === val);
if (chosenObject) {
//
col.injectTo.map((item) => {
this.$set(this.dataForm, item[0], chosenObject[item[1]]);
this.$forceUpdate();
});
} else {
console.log("DialogJustForm mounted(): 没有找到 injectTo 关联的对象");
}
},
{
immediate: false,
}
);
this.watchList.push(unwatch);
}
if ("autoUpdateProp" in col) {
this.$set(savedDatalist, col.prop, res.data.list);
}
// end branch
} else if (Array.isArray(res.data)) {
//
if ("injectTo" in col) {
//
this.cachedList[col.prop] = res.data;
}
this.$set(
col,
"options",
res.data.map((i) => ({
label: col.optionLabel ? i[col.optionLabel] : i.name,
value: col.optionValue ? i[col.optionValue] : i.id,
}))
);
if ("autoUpdateProp" in col) {
this.$set(savedDatalist, col.prop, res.data);
}
}
},
{
immediate: false,
} else {
col.options.splice(0);
}
);
this.watchList.push(unwatch);
});
}
if ("toggleFetchData" in col) {
if (col.fetchData && typeof col.fetchData === "function" && col.hasPrev) {
// -
// 1. fullfilled
// 2. fetchData
const unwatch = this.$watch(
() => this.dataForm[col.toggleFetchData],
(val) => {
@ -318,12 +371,10 @@ export default {
if ("autoUpdateProp" in col) {
this.$set(savedDatalist, col.prop, res.data);
}
}
// col.options = res.data.list;
}
} else {
col.options.splice(0);
}
// dataForm[col.prop] = col.default ?? null; // not perfect!
}
});
},
{
@ -332,6 +383,8 @@ export default {
);
this.watchList.push(unwatch);
}
/// old part ====================
});
});
},

View File

@ -150,7 +150,8 @@ export default function () {
prop: "orderId",
options: [],
optionLabel: 'orderCode',
toggleFetchData: 'carId', // 当 carId 改变的时候,也会 fetchData
// toggleFetchData: 'carId', // 当 carId 改变的时候,也会 fetchData
hasPrev: 'carId', // 当 carId 改变的时候,也会 fetchData
// 需要保存当前状态
needCache: true,
fetchDataParam: { search: 'carId', get: 'id' }, // 伴随着 toggleFetchData 出现