From 601949aaf0d818bb8c2c8b250be53ce615b0ecb4 Mon Sep 17 00:00:00 2001 From: Marvin Frederickson Date: Wed, 1 Mar 2017 09:09:49 -0900 Subject: [PATCH 1/2] closes #15 upgrade to turbolinks v5 --- app/assets/javascripts/concerto_calendar/concerto_calendar.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/assets/javascripts/concerto_calendar/concerto_calendar.js b/app/assets/javascripts/concerto_calendar/concerto_calendar.js index f24d3e2..51938ba 100644 --- a/app/assets/javascripts/concerto_calendar/concerto_calendar.js +++ b/app/assets/javascripts/concerto_calendar/concerto_calendar.js @@ -20,5 +20,4 @@ function attachHandlers() { $("input#calendar_config_end_date.datefield").datepicker(); } -$(document).ready(attachHandlers); -$(document).on('page:change', attachHandlers); +$(document).on('turbolinks:load', attachHandlers); From 0fbb897cdb78a71f11a4febb64d4f2f1fcaf66ea Mon Sep 17 00:00:00 2001 From: Marvin Frederickson Date: Wed, 1 Mar 2017 14:08:51 -0900 Subject: [PATCH 2/2] isolate namespace and load on ready --- .../concerto_calendar/concerto_calendar.js | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/concerto_calendar/concerto_calendar.js b/app/assets/javascripts/concerto_calendar/concerto_calendar.js index 51938ba..5d42975 100644 --- a/app/assets/javascripts/concerto_calendar/concerto_calendar.js +++ b/app/assets/javascripts/concerto_calendar/concerto_calendar.js @@ -1,7 +1,7 @@ -function attachHandlers() { - $('select#calendar_config_calendar_source').on('change', revealRelevantFields); +var ConcertoCalendar = { + _initialized: false, - function revealRelevantFields() { + revealRelevantFields: function () { var vendor = $('select#calendar_config_calendar_source').val(); if (vendor == 'google') { $('input#calendar_config_api_key').closest('div.clearfix').show(); @@ -12,12 +12,22 @@ function attachHandlers() { $('input#calendar_config_calendar_id').closest('div.clearfix').hide(); $('input#calendar_config_calendar_url').closest('div.clearfix').show(); } - } + }, - revealRelevantFields(); + initHandlers: function () { + if (ConcertoCalendar._initialized) { + // console.debug('already initialized Calendar handlers'); + } else { + // console.debug('initializing Calendar Handlers'); + $('select#calendar_config_calendar_source').on('change', ConcertoCalendar.revealRelevantFields); + ConcertoCalendar.revealRelevantFields(); - $("input#calendar_config_start_date.datefield").datepicker(); - $("input#calendar_config_end_date.datefield").datepicker(); -} + $("input#calendar_config_start_date.datefield").datepicker(); + $("input#calendar_config_end_date.datefield").datepicker(); + ConcertoCalendar._initialized = true; + } + } +}; -$(document).on('turbolinks:load', attachHandlers); +$(document).ready(ConcertoCalendar.initHandlers); +$(document).on('turbolinks:load', ConcertoCalendar.initHandlers);