From adc93e85990b644194c679f528225deed9fdaa85 Mon Sep 17 00:00:00 2001 From: Julian Andrews Date: Tue, 15 Mar 2016 10:11:34 -0600 Subject: [PATCH] Fixed #26357 -- Allowed admin popups to work on links added after page load. --- .../admin/static/admin/js/admin/RelatedObjectLookups.js | 2 +- django/contrib/admin/static/admin/js/change_form.js | 2 +- docs/internals/contributing/writing-code/javascript.txt | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js b/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js index d2dda892b1..3fb1e52550 100644 --- a/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js +++ b/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js @@ -162,7 +162,7 @@ } }); $('.related-widget-wrapper select').trigger('change'); - $('.related-lookup').click(function(e) { + $('body').on('click', '.related-lookup', function(e) { e.preventDefault(); var event = $.Event('django:lookup-related'); $(this).trigger(event); diff --git a/django/contrib/admin/static/admin/js/change_form.js b/django/contrib/admin/static/admin/js/change_form.js index 994b52394e..4797383b30 100644 --- a/django/contrib/admin/static/admin/js/change_form.js +++ b/django/contrib/admin/static/admin/js/change_form.js @@ -4,7 +4,7 @@ 'use strict'; $(document).ready(function() { var modelName = $('#django-admin-form-add-constants').data('modelName'); - $('.add-another').click(function(e) { + $('body').on('click', '.add-another', function(e) { e.preventDefault(); var event = $.Event('django:add-another-related'); $(this).trigger(event); diff --git a/docs/internals/contributing/writing-code/javascript.txt b/docs/internals/contributing/writing-code/javascript.txt index 25a46695c3..3d10c48f00 100644 --- a/docs/internals/contributing/writing-code/javascript.txt +++ b/docs/internals/contributing/writing-code/javascript.txt @@ -24,6 +24,12 @@ Code style JSHint will be run when you run the JavaScript tests. We also recommended installing a JSHint plugin in your text editor. +* Where possible, write code that will work even if the page structure is later + changed with JavaScript. For instance, when binding a click handler, use + ``$('body').on('click', selector, func)`` instead of + ``$(selector).click(func)``. This makes it easier for projects to extend + Django's default behavior with JavaScript. + .. _javascript-patches: JavaScript patches