Skip to content

Commit 25f2654

Browse files
committed
fix: update to how action is handled
1 parent 730bced commit 25f2654

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

src/form.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ function disableAutoFill(element) {
9393
/**
9494
* @param form
9595
*/
96-
function reset(form) {
96+
function reset(action) {
97+
let form = action.form;
98+
9799
// Convert the form elements collection to an array
98100
const formElementsArray = Array.from(form.elements);
99101

@@ -141,7 +143,7 @@ function reset(form) {
141143
});
142144

143145
// Dispatch a custom reset event
144-
document.dispatchEvent(
146+
action.element.dispatchEvent(
145147
new CustomEvent("reset", {
146148
detail: {}
147149
})
@@ -203,7 +205,7 @@ Action.init({
203205
name: "reset",
204206
endEvent: "reset",
205207
callback: (action) => {
206-
if (action.form) reset(action.form);
208+
if (action.form) reset(action);
207209
}
208210
});
209211

src/index.js

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ async function getData(form) {
10101010
return dataArray;
10111011
}
10121012

1013-
async function save(element) {
1013+
async function save(element, action) {
10141014
if (!element) return;
10151015
let data, value;
10161016
let upsert = element.getAttribute("upsert");
@@ -1116,19 +1116,28 @@ async function save(element) {
11161116
(data[i].type !== "object" &&
11171117
data[i].method.endsWith(".update")))
11181118
) {
1119-
setTypeValue(element, data[i]);
1120-
} else if (data[i])
1121-
document.dispatchEvent(
1122-
new CustomEvent("saved", {
1123-
detail: data[i]
1124-
})
1125-
);
1119+
setTypeValue(element, data[i], action);
1120+
} else if (data[i]) {
1121+
if (action) {
1122+
action.element.dispatchEvent(
1123+
new CustomEvent("saved", {
1124+
detail: data
1125+
})
1126+
);
1127+
} else {
1128+
document.dispatchEvent(
1129+
new CustomEvent("saved", {
1130+
detail: data
1131+
})
1132+
);
1133+
}
1134+
}
11261135
}
11271136

11281137
return Data;
11291138
}
11301139

1131-
function setTypeValue(element, data) {
1140+
function setTypeValue(element, data, action) {
11321141
// TODO: if an array name is updated, the attibute array="" needs to be updated.
11331142

11341143
if (!data) return;
@@ -1164,7 +1173,7 @@ function setTypeValue(element, data) {
11641173
}
11651174
setData(Array.from(elements.keys()), data);
11661175

1167-
// const state_ids = new Map();
1176+
// const state_ids = new Map()
11681177

11691178
// let state_id = form.getAttribute('state_id');
11701179
// if (state_id) {
@@ -1217,11 +1226,19 @@ function setTypeValue(element, data) {
12171226
// }
12181227
}
12191228

1220-
document.dispatchEvent(
1221-
new CustomEvent("saved", {
1222-
detail: data
1223-
})
1224-
);
1229+
if (action) {
1230+
action.element.dispatchEvent(
1231+
new CustomEvent("saved", {
1232+
detail: data
1233+
})
1234+
);
1235+
} else {
1236+
document.dispatchEvent(
1237+
new CustomEvent("saved", {
1238+
detail: data
1239+
})
1240+
);
1241+
}
12251242
}
12261243

12271244
async function remove(element) {
@@ -1503,7 +1520,7 @@ Actions.init([
15031520
name: "save",
15041521
endEvent: "saved",
15051522
callback: (action) => {
1506-
if (action.form) save(action.form);
1523+
if ((action.form, action)) save(action.form, action);
15071524
}
15081525
},
15091526
{
@@ -1546,7 +1563,7 @@ Actions.init([
15461563
data[data.type] = { _id: data[data.type] };
15471564

15481565
let response = await CRUD.send(data);
1549-
document.dispatchEvent(
1566+
action.element.dispatchEvent(
15501567
new CustomEvent("deleted", {
15511568
detail: response
15521569
})

0 commit comments

Comments
 (0)