Refactor multiple references to $(document)

According to the Horizon Contributing Guide, we are to:
"Avoid creating instances of the same object repeatedly
within the same scope. Instead, assign the object to a
variable and re-use the existing object."

The guide references the use of "$(document)", as an
example. And yet multiple examples can be found of that
sort of approach. This commit cleans up those references.

Partial-Bug: 1412971
Change-Id: Ica05700101ae9a453623d928f07b75fa7aaca186
This commit is contained in:
Chris Johnson 2015-03-09 10:59:34 -04:00
parent cba98e5b84
commit 2a8cad0674
2 changed files with 15 additions and 9 deletions

View File

@ -211,8 +211,11 @@ horizon.addInitFunction(horizon.forms.init = function () {
/* Switchable Fields (See Horizon's Forms docs for more information) */
// Single reference
var $document = $(document);
// Bind handler for swapping labels on "switchable" select fields.
$(document).on("change", 'select.switchable', function (evt) {
$document.on("change", 'select.switchable', function (evt) {
var $fieldset = $(evt.target).closest('fieldset'),
$switchables = $fieldset.find('select.switchable');
@ -247,7 +250,7 @@ horizon.addInitFunction(horizon.forms.init = function () {
});
// Bind handler for swapping labels on "switchable" checkbox input fields.
$(document).on("change", 'input.switchable', function (evt) {
$document.on("change", 'input.switchable', function (evt) {
var $fieldset = $(evt.target).closest('fieldset'),
$switchables = $fieldset.find('input.switchable');
@ -320,7 +323,7 @@ horizon.addInitFunction(horizon.forms.init = function () {
});
}
$(document).on('change', '#id_volume_source_type', function (evt) {
$document.on('change', '#id_volume_source_type', function (evt) {
update_volume_source_displayed_fields(this);
});
@ -332,7 +335,7 @@ horizon.addInitFunction(horizon.forms.init = function () {
/* Help tooltips */
// Apply standard handler for everything but checkboxes.
$(document).tooltip({
$document.tooltip({
selector: "div.form-group .help-icon",
placement: function (tip, input) {
// Position to the right unless this is a "split" for in which case put
@ -345,11 +348,11 @@ horizon.addInitFunction(horizon.forms.init = function () {
});
// Hide the tooltip upon interaction with the field for select boxes.
// We use mousedown and keydown since those "open" the select dropdown.
$(document).on('mousedown keydown', '.form-group select', function (evt) {
$document.on('mousedown keydown', '.form-group select', function (evt) {
$(this).tooltip('hide');
});
// Hide the tooltip after escape button pressed
$(document).on('keydown.esc_btn', function (evt) {
$document.on('keydown.esc_btn', function (evt) {
if (evt.keyCode === 27) {
$('.tooltip').hide();
}

View File

@ -44,9 +44,12 @@ horizon.addInitFunction(horizon.tabs.init = function () {
$(el).find(".js-tab-pane").addClass("tab-pane");
});
$(document).on("show.bs.tab", ".ajax-tabs a[data-loaded='false']", horizon.tabs.load_tab);
$(document).on("shown.bs.tab", ".nav-tabs a[data-toggle='tab']", function (evt) {
var $document = $(document);
$document.on("show.bs.tab", ".ajax-tabs a[data-loaded='false']", horizon.tabs.load_tab);
$document.on("shown.bs.tab", ".nav-tabs a[data-toggle='tab']", function (evt) {
var $tab = $(evt.target),
$content = $($(evt.target).attr('data-target'));
$content.find("table.datatable").each(function () {
@ -88,7 +91,7 @@ horizon.addInitFunction(horizon.tabs.init = function () {
}
});
$(document).on("focus", ".tab-content :input", function () {
$document.on("focus", ".tab-content :input", function () {
var $this = $(this),
tab_pane = $this.closest(".tab-pane"),
tab_id = tab_pane.attr('id');