开发者

rails run first query over ajax (after view is loaded)

I have pagination + parameters set up over ajax, and working properly.

However, when first loading the page, it takes a开发者_如何学C while. Is it possible to first load the view, and then load the content over ajax (just like clicking page 2 would for pagination over ajax)?

EDIT/

Is it really stupid to use a parameter conditional? Like in my controller around my query if params[:load] == "ajax" as well as the parts of the view that use this data. And in jquery, when the document is ready (view loaded), call the parameter with a get: $.getScript(window.location.pathname + "?load=ajax"); ???


I have done it quite a lot. Load the whole view first, which contains some basic layout, then load the actual important contents using ajax. So in my controller:

class PriceController < ApplicationController

  def price
    # do something in order to render the page price.html
  end

  def price_f
   # render the partial which contains heavy sorting of datas
   render :partial => "price_f"
  end

end

in jquery

$(document).ready(function(){
  $.ajax({
    url : "/price_f.html?page=1"
    success: function(data){
       $("#price_container").html(data);
    }
  });
})


Assuming you're using jQuery, you should be able to just do something simple like this after the document is ready. Really, all you're doing is telling your pagination URL that you want Page 1 instead of Page 2, etc.

$(document).ready(function(){
  $.get({
    url: "/url/for/your/page/data"
  });
});

To get rid of the current initial page load (I would have to see your specific code to tell you exactly what to remove/change):

  1. Remove whatever code is in your controller that does the query, loads the data, etc.
  2. Remove the parts of your view that are then using this initial data, maybe just sticking a loading gif in there until it's replaced by your ajax data.
  3. Fire the ajax request -- the page will be loaded while it waits for the controller to sort and deliver the data.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜