Skip to content

Commit 66a634a

Browse files
committed
Fix: Handle undefined django object to prevent TypeError
- Added a check to ensure `django` is defined before accessing `django.jQuery`. - This prevents the "Uncaught TypeError: Cannot read properties of undefined (reading 'jQuery')" error.
1 parent e3c8df6 commit 66a634a

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

ajax_select/static/ajax_select/js/ajax_select.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -231,25 +231,27 @@
231231
* In django >= 1.10 we could try to rely on input.trigger('change')
232232
* and avoid this hijacking, but the id and repr isn't passed.
233233
*/
234-
djangoJQuery(document).ready(function () {
235-
var djangoDismissAddRelatedObjectPopup =
236-
window.dismissAddRelatedObjectPopup;
237-
if (!djangoDismissAddRelatedObjectPopup) {
238-
throw new Error("dismissAddAnotherPopup not found");
239-
}
240-
241-
window.dismissAddRelatedObjectPopup = function (win, newId, newRepr) {
242-
// Iff this is an ajax-select input then close the window and
243-
// trigger didAddPopup
244-
var input = $("#" + win.name.replace("__1", ""));
245-
if (input.length && input.data("ajax-select")) {
246-
win.close();
247-
// note: newRepr is django's repr of object, not the Lookup's formatting of it.
248-
input.trigger("didAddPopup", [newId, newRepr]);
249-
} else {
250-
// Call the normal django set and close function.
251-
djangoDismissAddRelatedObjectPopup(win, newId, newRepr);
234+
if (djangoJQuery) {
235+
djangoJQuery(document).ready(function () {
236+
var djangoDismissAddRelatedObjectPopup =
237+
window.dismissAddRelatedObjectPopup;
238+
if (!djangoDismissAddRelatedObjectPopup) {
239+
throw new Error("dismissAddAnotherPopup not found");
252240
}
253-
};
254-
});
255-
})(window.jQuery, window.django.jQuery);
241+
242+
window.dismissAddRelatedObjectPopup = function (win, newId, newRepr) {
243+
// Iff this is an ajax-select input then close the window and
244+
// trigger didAddPopup
245+
var input = $("#" + win.name.replace("__1", ""));
246+
if (input.length && input.data("ajax-select")) {
247+
win.close();
248+
// note: newRepr is django's repr of object, not the Lookup's formatting of it.
249+
input.trigger("didAddPopup", [newId, newRepr]);
250+
} else {
251+
// Call the normal django set and close function.
252+
djangoDismissAddRelatedObjectPopup(win, newId, newRepr);
253+
}
254+
};
255+
});
256+
}
257+
})(window.jQuery, window.django && window.django.jQuery);

0 commit comments

Comments
 (0)