2013-06-05 79 views
1

我寫了這個函數處理數據進來並用相應的數據填充表單。我不知道是否有更好的方法來簡單地驗證碼:簡化jQuery功能

//Handling of "regular weeks" (no "blackouts" or empty days) 
    //Itteration through specific day # to ensure specificity 
    for (var a = 0; a < data.days.length; a++) { 
     var default_start_time = data.days[a].default_start_time; 
     var default_end_time = data.days[a].default_end_time; 
     var select_start_time = data.days[a].start_time; 
     var select_end_time = data.days[a].end_time; 
     var time_chunk = data.days[a].time_chunk_id; 

     if (data.days[a].day_of_week_Number == 2) { 
      $("#mondayHead").html("Monday"); 
      $("#mondayDate").html(data.days[a].date); 
      $("#mondayStart").html(buid(default_start_time, default_end_time, select_start_time)); 
      $("#mondayEndTime").html(buid(default_start_time, default_end_time, select_end_time)); 
      $("#mondayTimeChunkID").val(time_chunk); 
      if (data.days[a].comment != null) { 
       var subMonday = data.days[a].comment.substring(0, 10); 
       //Comment is held not diplayed **to be fixed 
       $("#mondayCommentLink").html(subMonday + "..." + "<input type='hidden' id='mondayCommentText' name='mondayCommentText' size='10' value=data.days[a].comment />"); 
      } 
     } 

     if (data.days[a].day_of_week_Number == 3) { 
      $("#tuesdayHead").html("Tuesday"); 
      $("#tuesdayDate").html(data.days[a].date); 
      $("#tuesdayStart").html(buid(default_start_time, default_end_time, select_start_time)); 
      $("#tuesdayEndTime").html(buid(default_start_time, default_end_time, select_end_time)); 
      $("#tuesdayTimeChunkID").val(time_chunk); 
      if (data.days[a].comment != null) { 
       var subTuesday = data.days[a].comment.substring(0, 10); 
       $("#tuesdayCommentLink").html(subTuesday + "..."); 
      } 
     } 

     if (data.days[a].day_of_week_Number == 4) { 
      $("#wednesdayHead").html("Wednesday"); 
      $("#wednesdayDate").html(data.days[a].date); 
      $("#wednesdayStart").html(buid(default_start_time, default_end_time, select_start_time)); 
      $("#wednesdayEndTime").html(buid(default_start_time, default_end_time, select_end_time)); 
      $("#wednesdayTimeChunkID").val(time_chunk); 
      if (data.days[a].comment != null) { 
       var subWednesday = data.days[a].comment.substring(0, 10); 
       $("#wednesdayCommentLink").html(subWednesday + "..."); 
      } 
     } 

     if (data.days[a].day_of_week_Number == 5) { 
      $("#thursdayHead").html("Thursday"); 
      $("#thursdayDate").html(data.days[a].date); 
      $("#thursdayStart").html(buid(default_start_time, default_end_time, select_start_time)); 
      $("#thursdayEndTime").html(buid(default_start_time, default_end_time, select_end_time)); 
      $("#thursdayTimeChunkID").val(time_chunk); 
      if (data.days[a].comment != null) { 
       var subThursday = data.days[a].comment.substring(0, 10); 
       $("#thursdayCommentLink").html(subThursday + "..."); 
      } 
     } 

     if (data.days[a].day_of_week_Number == 6) { 
      $("#fridayHead").html("Friday"); 
      $("#fridayDate").html(data.days[a].date); 
      $("#fridayStart").html(buid(default_start_time, default_end_time, select_start_time)); 
      $("#fridayEndTime").html(buid(default_start_time, default_end_time, select_end_time)); 
      $("#fridayTimeChunkID").val(time_chunk); 
      if (data.days[a].comment != null) { 
       var subFriday = data.days[a].comment.substring(0, 10); 
       $("#fridayCommentLink").html(subFriday + "..."); 
      } 
     } 

     if (data.days[a].day_of_week_Number == 7) { 
      $("#saturdayHead").html("Saturday"); 
      $("#saturdayDate").html(data.days[a].date); 
      $("#saturdayStart").html(buid(default_start_time, default_end_time, select_start_time)); 
      $("#saturdayEndTime").html(buid(default_start_time, default_end_time, select_end_time)); 
      $("#saturdayTimeChunkID").val(time_chunk); 
      if (data.days[a].comment != null) { 
       var subSaturday = data.days[a].comment.substring(0, 10); 
       $("#saturdayCommentLink").html(subSaturday + "..."); 
      } 
     } 

    } 

林好奇,如果有使用兩個for循環並反覆通過這個功能來更新這些特定值的方式。我覺得擁有一大堆if陳述並不是一個好習慣,在看它時也不是很優雅。

+2

嘗試在http://codereview.stackexchange.com/發佈此。這似乎是「iditis」的常見情況。可以考慮使用普通的類。 – elclanrs

回答

4

有這樣一個婷的開關/箱,但對於這個天的目標似乎更爲合適:

for (var a = 0; a < data.days.length; a++) { 
    var default_start_time = data.days[a].default_start_time, 
     default_end_time = data.days[a].default_end_time, 
     select_start_time = data.days[a].start_time, 
     select_end_time = data.days[a].end_time, 
     time_chunk   = data.days[a].time_chunk_id, 
     days    = {2: 'monday', 3: 'tuesday', 4: 'wednesday', 5: 'thursday', 6: 'friday', 7: 'saturday'}, 
     today    = days[data.days[a].day_of_week_Number]; 

    $("#"+today+"Head").html(today.charAt(0).toUpperCase() + today.slice(1)); 
    $("#"+today+"Date").html(data.days[a].date); 
    $("#"+today+"Start").html(buid(default_start_time, default_end_time, select_start_time)); 
    $("#"+today+"EndTime").html(buid(default_start_time, default_end_time, select_end_time)); 
    $("#"+today+"TimeChunkID").val(time_chunk); 
    if (data.days[a].comment != null) { 
     var subMonday = data.days[a].comment.substring(0, 10); 
     $("#"+today+"CommentLink").html(subMonday + "..." + "<input type='hidden' id='"+today+"CommentText' name='"+today+"CommentText' size='10' value=data.days[a].comment />"); 
    } 
}