這是我怎麼會在你的js.coffee文件結構這樣的任務
get '/your_route', to: 'controller#your_action', as: :named_route
在控制器
def your_action
respond_to do |format|
format.html # this line will just load the page
format.json {render json: your_data_load.to_json} #this line will actually supply your data when you request a json response
end
end
$ ->
#start your loading image
$.getJSON '/your_route.json', (data) ->
#populate the page with your data
#remove your loading image
什麼這是當頁面被加載時,它會呈現HTML頁面,因爲respond_to
塊,然後當頁面加載時,它會啓動您的加載圖像。然後,它會向相同的路線發出請求,但是會發出json
請求,該請求實際上也會通過respond_to
塊提供您的信息。這個json對象將包含在jQuery中的數據變量中,您可以根據需要處理它,然後在完成後刪除加載圖像。
如果您提供了更多信息,我的答案可能會更不明確,但這是我將用於處理您的請求的樣式。
這不是可擴展的嘗試「加載所有的結果」,除非那些僅限於只是適合那些在頁面上。指定要返回的特定偏移量和結果數量通常很容易,因此可以輕鬆遍歷頁面大小的塊。 – 2014-08-27 23:18:33