2014-10-16 26 views
0

嘗試爲Fullcalendar呈現器加載事件JSON流時,我繼承維護的應用程序超時。在月視圖中每週(或每個塊)啓用一個請求

我希望我會得到批准,在問題上拋出一些更多的計算資源,希望能夠解決它,但除此之外,我很難找出如何解決這個問題。

我的一個想法是將數據請求分成小塊,每次請求1周的數據(而不是1個月)。

這似乎應該是可行的,但我無法弄清楚如何趕上時間表的變化,將其拆分成更小的塊,然後發出多個請求,而不是一個。

+0

聽起來像是異步進程的工作。但很難給出具體細節,而不知道你正在使用什麼平臺......這是一個網絡應用程序?你在使用什麼平臺/語言? – Eric 2014-10-16 19:19:32

+0

您確定問題不在於您使用的是舊版本的fullcalendar嗎?我在這裏看到了這個凍結問題:http://jsfiddle.net/jcornelius/pba56nf1/,已在新版本中解決:http://jsfiddle.net/pba56nf1/2/ – 2014-10-16 22:07:52

+0

對不起,我沒有清晰度還沒有使用過StackOverflow。這是一個使用Ruby on Rails構建的Web應用程序。 – CallMeEsmale 2014-10-17 02:15:32

回答

0

看來你試圖解決的主要問題是爲什麼它超時?不知道很多細節,它可能是簡單的作爲數據庫索引或服務器上的某個其他進程正在消耗許多資源。我建議你分析你的數據庫和框架/服務器來解決這個問題。

這就是說,你可以做兩件事情:

  1. 禁用month以迫使FullCalendar使用agendaWeekagendaDay。類似這樣的:

    $('#calendar').fullCalendar({ 
        header: { 
         left: 'prev,next', 
         center: 'title', 
         right: 'agendaWeek,agendaDay' 
        }, 
        defaultView: 'agendaWeek' 
    }); 
    

    See an example

  2. 如果您真的需要month查看,您可以使用events as a function。您必須計算月份每週的開始和結束日期,然後調用回調。事情是這樣的:

    $('#calendar').fullCalendar({ 
        events: function(start, end, timezone, callback) { 
         // do this in chunks only when view is month 
         if ($("#calendar").fullCalendar('getView').name == 'month') { 
          // You'll need to get the start and end day of each week. 
          // The parameters `start` amd `end` relate to the first day of 
          // the month and the last day of the month. 
    
          // You should get an array of objects, like this 
          var weeks = [ 
           {start: '2014-09-28', end: '2014-10-04'}, 
           {start: '2014-10-05', end: '2014-10-11'}, 
           {start: '2014-10-12', end: '2014-10-18'}, 
           {start: '2014-10-19', end: '2014-10-25'}, 
           {start: '2014-10-26', end: '2014-11-01'} 
          ]; 
    
          // When you have that: 
          for(var i in weeks) { 
           getEvents(weeks[i].start, weeks[i].end, callback); 
          } 
         } 
         // if its another view, get all events at once 
         else { 
          getEvents(start, end, callback); 
         } 
        } 
    }); 
    
    function getEvents(start, end, callback) { 
        $.ajax({ 
         url: 'events', 
         dataType: 'json', 
         async: false, 
         data: { 
          start: start, 
          end: end 
         }, 
         success: function(events) { 
          callback(events); 
         } 
        }); 
    } 
    

    爲了計算一週的開始和結束時,你可以看一下這個問題:get next week start and end using jquery and moment js,它應該幫助你。

    請注意,我已將async: false添加到$.ajax以便一次只有一個請求。

    另請注意,此代碼適用於FullCalendar 2。*。既然你正在談論現有的應用程序,有些事情會有所不同。 documentation for events of version 1.*指出簽名有點不同:function(start, end, callback) { }

+0

我懷疑這個問題真的是提取事件的數量,除非有數千個事件,即使那樣,仍然存在疑問。渲染是需要花費時間的。 – 2014-10-16 22:10:28

+0

@RichardHermanson和我一樣,這就是爲什麼我建議OP做一些分析。沒有附加的細節,幾乎不可能說明是什麼導致了這個問題。 – 2014-10-16 22:15:36

+0

Sry,我有閱讀代碼而不是文字的壞習慣:) – 2014-10-16 22:18:19

相關問題