I am very new to JS & Jquery. I am building a scheduling app. I am storing appointments in a model called "Event". I am using a helper to build the multi-dimensional array from values in columns called "event.time_start" and "event.time_end" for a given day that a user might make an appointment. When a user picks a start_time and end_time from the dropdown menu, times that already have appointments will be grayed out/"disabled". I am using jquery.timepicker by http://jonthornton.github.io/jquery-timepicker/ with rails to blocks certain time values in a given array of time pairs ( [8am, 6pm], [1pm, 2pm]).
I am finding the "other" events on a given day (the ones that inform which times are taken) using the variable @other_events.
this is the helper that pairs the event.start_time and event.end_time
events_helper.erb
module EventsHelper
def taken_times(other_events)
other_events.map { |event| [event.time_start, event.time_end] }
@taken_times = taken_times(other_events)
end
end
I can get the array to work like this in the view:
<% taken_times(@other_events).each do |taken_time| %>
<%= taken_time.first.strftime("%l:%M %P")%>
<%= taken_time.last.strftime("%l:%M %P")%>,
<% end %>
But I can't get it to work in the JS. I am now trying to use the Gon Gem What I need to out put to is this:
events/show.html.erb view
<script>
$('#jqueryExample .time').timepicker({
'timeFormat': 'g:ia',
'disableTimeRanges': [
// start dynamic array
['10:00am', '10:15am'],
['1:00pm', '2:15pm'],
['5:00pm', '5:15pm']
]
//end dynamic array
});
$('#jqueryExample').datepair();
</script>
so I've tried
application.html.erb
<%= Gon::Base.render_data({}) %>
events_controller.erb
def show
@event = Event.new
@other_events = Event.where(date_id: params[:id])
gon.taken_times = @taken_times
end
with the JS
<script>
$('#jqueryExample .time').timepicker({
'timeFormat': 'g:ia',
'disableTimeRanges': [
// start dynamic array
gon.taken_times
//end dynamic array
});
$('#jqueryExample').datepair();
</script>
but this doesn't work. it just kills the dropdown function of the textfields. I'd love to know what these comma separated objects like "'timeFormat': 'g:ia'," and "disableTimeRanges: [....]" are called so I can better search SO for javascript answers. I am very new to JS & JQuery.