Skip to content

Commit

Permalink
Fix: Handle undefined django object to prevent TypeError. Fixes: #313
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
SukiCZ committed Sep 3, 2024
1 parent e3c8df6 commit 3e6ba31
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions ajax_select/static/ajax_select/js/ajax_select.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,25 +231,28 @@
* In django >= 1.10 we could try to rely on input.trigger('change')
* and avoid this hijacking, but the id and repr isn't passed.
*/
djangoJQuery(document).ready(function () {
var djangoDismissAddRelatedObjectPopup =
window.dismissAddRelatedObjectPopup;
if (!djangoDismissAddRelatedObjectPopup) {
throw new Error("dismissAddAnotherPopup not found");
}

window.dismissAddRelatedObjectPopup = function (win, newId, newRepr) {
// Iff this is an ajax-select input then close the window and
// trigger didAddPopup
var input = $("#" + win.name.replace("__1", ""));
if (input.length && input.data("ajax-select")) {
win.close();
// note: newRepr is django's repr of object, not the Lookup's formatting of it.
input.trigger("didAddPopup", [newId, newRepr]);
} else {
// Call the normal django set and close function.
djangoDismissAddRelatedObjectPopup(win, newId, newRepr);
if (djangoJQuery) {
// only for Django Admin
djangoJQuery(document).ready(function () {
var djangoDismissAddRelatedObjectPopup =
window.dismissAddRelatedObjectPopup;
if (!djangoDismissAddRelatedObjectPopup) {
throw new Error("dismissAddAnotherPopup not found");
}
};
});
})(window.jQuery, window.django.jQuery);

window.dismissAddRelatedObjectPopup = function (win, newId, newRepr) {
// Iff this is an ajax-select input then close the window and
// trigger didAddPopup
var input = $("#" + win.name.replace("__1", ""));
if (input.length && input.data("ajax-select")) {
win.close();
// note: newRepr is django's repr of object, not the Lookup's formatting of it.
input.trigger("didAddPopup", [newId, newRepr]);
} else {
// Call the normal django set and close function.
djangoDismissAddRelatedObjectPopup(win, newId, newRepr);
}
};
});
}
})(window.jQuery, window.django && window.django.jQuery);

0 comments on commit 3e6ba31

Please sign in to comment.