Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Removed 'return false' in favor of preventDefault. #24

Merged
merged 1 commit into from almost 2 years ago

7 participants

Marc Neuwirth Adrian Holovaty Eduardo Cereto Carvalho Mark Lavin Alex Gaynor Jannis Leidel Julien Phalip
Marc Neuwirth

Using 'return false' makes it very difficult to add additional event handlers to the add button because it prevent the events from bubbling up; preventDefault appears to be the desired behavior.

Also, other various js fixes like === instead of ==, missing parseInt radix, and missing semi colons

https://code.djangoproject.com/ticket/18241

Adrian Holovaty
Owner

Hey, this is looking good. I have one question, though -- should the preventDefault calls perhaps be at the start of the event handler instead of the end? That way, if there's a runtime error in the handler, the link won't get followed (even though its href is "#").

See the Stack Overflow answer by Jeff Poulton here for a better explanation: http://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false

Worth changing?

Marc Neuwirth

Good idea

Eduardo Cereto Carvalho

Note that internet explorer 8- doesn't support preventDefault. So you probably want to do this instead:

if (e.preventDefault) { 
    e.preventDefault(); 
} else {
    e.returnValue = false;
}
Mark Lavin
mlavin commented May 09, 2012

IE doesn't natively support e.preventDefault but this is will work inside a jQuery event handler because jQuery.Event defines preventDefault https://github.com/jquery/jquery/blob/1.4.2/src/event.js#L566

Alex Gaynor
Owner
alex commented May 10, 2012

Looks good to me, Adrian, are you good with this?

Adrian Holovaty
Owner

This looks good -- thanks, guys. I'd like us to start squashing our pull requests, though, so that I end up making only one commit to the Django master repository. @marcneuwirth -- can you squash your commits into one?

Adrian Holovaty adrianholovaty merged commit 04785d2 into from May 18, 2012
Adrian Holovaty adrianholovaty closed this May 18, 2012
Adrian Holovaty
Owner

Thank you!

Jannis Leidel
Owner
jezdez commented May 19, 2012

The minified version needs to be updated, too.

Julien Phalip
Owner

The minified version was updated in fadcc6d.

Jannis Leidel
Owner
jezdez commented June 17, 2012

Odd, pretty sure I updated the files, at the time, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

May 12, 2012
Marc Neuwirth Removed 'return false' in favor of preventDefault.
Moved preventDefault to the top
b719cbb
This page is out of date. Refresh to see the latest.
26  django/contrib/admin/static/admin/js/inlines.js
@@ -31,11 +31,11 @@
31 31
 			}
32 32
 		};
33 33
 		var totalForms = $("#id_" + options.prefix + "-TOTAL_FORMS").attr("autocomplete", "off");
34  
-		var nextIndex = parseInt(totalForms.val());
  34
+		var nextIndex = parseInt(totalForms.val(), 10);
35 35
 		var maxForms = $("#id_" + options.prefix + "-MAX_NUM_FORMS").attr("autocomplete", "off");
36 36
 		// only show the add button if we are allowed to add more items,
37 37
         // note that max_num = None translates to a blank string.
38  
-		var showAddButton = maxForms.val() == '' || (maxForms.val()-totalForms.val()) > 0;
  38
+		var showAddButton = maxForms.val() === '' || (maxForms.val()-totalForms.val()) > 0;
39 39
 		$(this).each(function(i) {
40 40
 			$(this).not("." + options.emptyCssClass).addClass(options.formCssClass);
41 41
 		});
@@ -52,13 +52,14 @@
52 52
 				$(this).filter(":last").after('<div class="' + options.addCssClass + '"><a href="javascript:void(0)">' + options.addText + "</a></div>");
53 53
 				addButton = $(this).filter(":last").next().find("a");
54 54
 			}
55  
-			addButton.click(function() {
  55
+			addButton.click(function(e) {
  56
+				e.preventDefault();
56 57
 				var totalForms = $("#id_" + options.prefix + "-TOTAL_FORMS");
57 58
 				var template = $("#" + options.prefix + "-empty");
58 59
 				var row = template.clone(true);
59 60
 				row.removeClass(options.emptyCssClass)
60  
-				    .addClass(options.formCssClass)
61  
-				    .attr("id", options.prefix + "-" + nextIndex);
  61
+					.addClass(options.formCssClass)
  62
+					.attr("id", options.prefix + "-" + nextIndex);
62 63
 				if (row.is("tr")) {
63 64
 					// If the forms are laid out in table rows, insert
64 65
 					// the remove button into the last table cell:
@@ -78,14 +79,15 @@
78 79
 				// Insert the new form when it has been fully edited
79 80
 				row.insertBefore($(template));
80 81
 				// Update number of total forms
81  
-				$(totalForms).val(parseInt(totalForms.val()) + 1);
  82
+				$(totalForms).val(parseInt(totalForms.val(), 10) + 1);
82 83
 				nextIndex += 1;
83 84
 				// Hide add button in case we've hit the max, except we want to add infinitely
84  
-				if ((maxForms.val() != '') && (maxForms.val()-totalForms.val()) <= 0) {
  85
+				if ((maxForms.val() !== '') && (maxForms.val()-totalForms.val()) <= 0) {
85 86
 					addButton.parent().hide();
86 87
 				}
87 88
 				// The delete button of each row triggers a bunch of other things
88  
-				row.find("a." + options.deleteCssClass).click(function() {
  89
+				row.find("a." + options.deleteCssClass).click(function(e) {
  90
+					e.preventDefault();
89 91
 					// Remove the parent form containing this button:
90 92
 					var row = $(this).parents("." + options.formCssClass);
91 93
 					row.remove();
@@ -98,7 +100,7 @@
98 100
 					var forms = $("." + options.formCssClass);
99 101
 					$("#id_" + options.prefix + "-TOTAL_FORMS").val(forms.length);
100 102
 					// Show add button again once we drop below max
101  
-					if ((maxForms.val() == '') || (maxForms.val()-forms.length) > 0) {
  103
+					if ((maxForms.val() === '') || (maxForms.val()-forms.length) > 0) {
102 104
 						addButton.parent().show();
103 105
 					}
104 106
 					// Also, update names and ids for all remaining form controls
@@ -110,17 +112,15 @@
110 112
 							updateElementIndex(this, options.prefix, i);
111 113
 						});
112 114
 					}
113  
-					return false;
114 115
 				});
115 116
 				// If a post-add callback was supplied, call it with the added form:
116 117
 				if (options.added) {
117 118
 					options.added(row);
118 119
 				}
119  
-				return false;
120 120
 			});
121 121
 		}
122 122
 		return this;
123  
-	}
  123
+	};
124 124
 	/* Setup plugin defaults */
125 125
 	$.fn.formset.defaults = {
126 126
 		prefix: "form",					// The form prefix for your django formset
@@ -132,5 +132,5 @@
132 132
 		formCssClass: "dynamic-form",	// CSS class applied to each form in a formset
133 133
 		added: null,					// Function called each time a new form is added
134 134
 		removed: null					// Function called each time a form is deleted
135  
-	}
  135
+	};
136 136
 })(django.jQuery);
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.