2014-07-01 67 views
0

我有一個頁面顯示數據庫中的可用日期。它運行良好,但我必須刷新頁面才能使其正常工作。如果我不這樣做,datepicker類就像一個文本框。另外,我需要它在顯示之前先做一個ajax調用。Datepicker ajax call beforeShow;不起作用

所以總結一下,我去'/任務/新'。頁面正確加載。我點擊日期選擇器字段並且沒有日曆。控制檯中沒有錯誤。我刷新頁面,它的工作原理。我需要做什麼?我最初的想法是,它需要等到頁面被加載,但這似乎並不奏效(除非我做錯了)。

下面是相關代碼:

HTML

<input class="datepicker" id="task_start_date" name="task[start_date]" type="text"> 

tasks.js.coffee

full_days =[] 
partial_days=[] 

getDays = (date) -> 
    mmddyyyy = ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + "-" + date.getFullYear() 
    jqxhr = $.get("/getdates/"+mmddyyyy, (data) -> 
    full_days = data['full'] 
    partial_days = data['partial'] 
    formatDays 
    return 
) 

formatDays = (date) -> 
    mmddyyyy = ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + "-" + date.getFullYear() 
    if $.inArray(mmddyyyy, full_days) isnt -1 
    [ 
     true 
     "full-day" 
     "Available" 
    ] 
    else 
    if $.inArray(mmddyyyy, partial_days) isnt -1 
     [ 
     true 
     "partial-day" 
     "Available" 
     ] 
    else 
     [ 
     false 
     "" 
     "unAvailable" 
     ] 

# manipulate the json to make an array 
availableHours = (day) -> 
    hours =[] 
    for index, hour_obj of day 
    hours.push hour_obj['hour'] 
    return hours 

$ -> 
    $('.datepicker').datepicker(
    dateFormat: 'mm-dd-yy' 
    beforeShow: -> getDays 
    beforeShowDay: formatDays 
    onChangeMonthYear: (year, month, inst) -> 
     target = "#{month}-01-#{year}" 
     jqxhr = $.get("/getdates/"+target, (data) -> 
     full_days = data['full'] 
     partial_days = data['partial'] 
     formatDays 
     return 
    ) 
    onSelect: (selectedDay) -> 
     box_id = $(this).attr('id') 
     box_id = "\##{(box_id.split '_')[1]}_hour" 
     new_list_items =[] 
     jqxhr2 = $.get('/gethours/'+selectedDay, (data)-> 
     hours = availableHours(data) 
     for k,hour of hours 
      new_list_items.push '<option>'+hour+'</option>' 
     $(box_id).html('').append(new_list_items) 
    ) 
) 
+1

是'beforeShow: - > getDays'應該是'beforeShow:getDays'或'beforeShow: - > getDays() '? –

+0

這幫助我找出問題的一部分。謝謝。 – Jeff

回答

1

我找到了一種方法來解決我的問題。這是所有關於我的時候調用的函數,當我把inital $ ->

$ -> 
    full_days = [] 
    partial_days = [] 

    getDays = (date) -> 
    if (typeof date != 'string') 
     date = new Date() 
     date = ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + "-" + date.getFullYear() 
    jqxhr = $.get("/getdates/" + date, (data) -> 
     full_days = data['full'] 
     partial_days = data['partial'] 
     formatDays 
    ) 
    return 

    getDays() 

    formatDays = (date) -> 
    mmddyyyy = ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + "-" + date.getFullYear() 
    if $.inArray(mmddyyyy, full_days) isnt -1 
     [ 
     true 
     "full-day" 
     "Available" 
     ] 
    else 
     if $.inArray(mmddyyyy, partial_days) isnt -1 
     [ 
      true 
      "partial-day" 
      "Available" 
     ] 
     else 
     [ 
      false 
      "" 
      "unAvailable" 
     ] 

    # manipulate the json to make an array 
    availableHours = (day) -> 
    hours = [] 
    for index, hour_obj of day 
     hours.push hour_obj['hour'] 
    return hours 

    showdatepicker = -> 
    $('.datepicker').datepicker(
     dateFormat: 'mm-dd-yy' 
     beforeShowDay: formatDays 
     onChangeMonthYear: (year, month, inst)-> 
     target = "#{month}-01-#{year}" 
     getDays target 
     onSelect: (selectedDay) -> 
     getDays() 
     box_id = $(this).attr('id') 
     box_id = "\##{(box_id.split '_')[1]}_hour" 
     new_list_items = [] 
     jqxhr2 = $.get('/gethours/' + selectedDay, (data)-> 
      hours = availableHours(data) 
      for k,hour of hours 
      new_list_items.push '<option>' + hour + '</option>' 
      $(box_id).html('').append(new_list_items) 
     ) 
    ) 

    showdatepicker()