开发者

UJS-like AJAX response in Rails 3

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜