Skip to content

Commit d73ee80

Browse files
committed
fix(pat-inject): Don't break when no target is found.
Don't break when no target can be found, but improve the error message and spit out the CSS selector causing the problem.
1 parent 1e08ee5 commit d73ee80

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/pat/inject/inject.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ const inject = {
318318
) {
319319
_confirm = this.elementIsDirty(cfg.$target);
320320
} else if (cfg.confirm === "class" && cfg.target && cfg.target !== "none") {
321-
_confirm = cfg.$target.hasClass("is-dirty");
321+
_confirm = cfg.$target?.hasClass("is-dirty");
322322
}
323323
if (_confirm) {
324324
should_confirm = true;
@@ -403,8 +403,8 @@ const inject = {
403403
// Special case, we don't want to display any return value.
404404
return;
405405
}
406-
const $form = cfg.$target.parents("form");
407-
if ($form.length !== 0 && cfg.$target.data("initial-value") === undefined) {
406+
const $form = cfg.$target?.parents("form");
407+
if ($form && $form.length !== 0 && cfg.$target.data("initial-value") === undefined) {
408408
cfg.$target.data("initial-value", cfg.$target.html());
409409
$form.on("reset", () => {
410410
cfg.$target.html(cfg.$target.data("initial-value"));
@@ -447,7 +447,10 @@ const inject = {
447447
* appended to the body.
448448
*/
449449
if (selector.slice(0, 1) !== "#") {
450-
log.error("only id supported for non-existing target");
450+
log.error(
451+
"only id supported for non-existing target. selector: ",
452+
selector
453+
);
451454
return null;
452455
}
453456
const $target = $("<div />").attr({ id: selector.slice(1) });
@@ -729,7 +732,7 @@ const inject = {
729732
// Add a loading class to the target.
730733
// Can be used for loading-spinners.
731734
if (cfg?.loadingClass && cfg?.target !== "none") {
732-
cfg.$target.addClass(cfg.loadingClass);
735+
cfg.$target?.addClass(cfg.loadingClass);
733736
}
734737
}
735738

0 commit comments

Comments
 (0)