开发者

UJS-like AJAX response in Rails 3

开发者 https://www.devze.com 2023-03-13 11:15 出处:网络
Rails 3 uses UJS approach, which allows to handle AJAX responses from JavaScript code. $(\"form\").live(\"ajax:success\", function(data, status, xhr){

Rails 3 uses UJS approach, which allows to handle AJAX responses from JavaScript code.

$("form").live("ajax:success", function(data, status, xhr){
    $(".target").html(data.myHTML);
});

How view in Rails 3 should look like to fill "data" object, so i can inspect is from JavaScript ?

Regular rendering, using "js.erb", or "js.haml" results "data" local variable in JavaScript "undefined".

Of course, in the view, i can use some global variable, like following ("js.haml"):

-开发者_如何学Go c = escape_javascript(render "search_result")
:plain
  var ajaxResponse = "#{c}";

… And call it from JavaScript handler, like this:

$("form").live("ajax:success", function(data, status, xhr){
    $(".target").html(ajaxResponse);
});

… but it breaks incapsulation.

Ideal way would be using "data" local variable.

So, how the view should look like ?

0

精彩评论

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