开发者

Rails - Access AJAX Triggering Element in Callback

I have a view, say show.js.erb. And I have a link in ano开发者_如何学Gother view such that

link_to "MyLink", my_object_path, :remote => true

successfully returns the show.js.erb view. My question is, from within that view, is there any way to access the element that triggered the AJAX call without having to resort to generating an id specific to individual elements a la ...

I want to be able to use this view callback to open a small dialog next to whatever element was clicked on, but I can't seem to find a way to access the triggering element.

I tried using $(this) but that doesn't work.

I'd like to do something along the lines of

$(this).after("some new html here");


My solution was to bind a pre-submit class to the element, in my case a popup modal window. It's a similar solution to the post linked to above in that it uses the pre-submit bindings, but tailored to use classes instead.

In public/javascripts/application.rb:

jQuery(function($) { 
  $(".poppable").bind("ajax:loading", function() { $(this).addClass("popped"); });
});

Then in my view for the popup content (e.g. app/views/mymodel/popup.js.erb):

var p = $(".poppable.popped");
p.removeClass("popped");
/* Do what I need to with p ... */

If this doesn't look kosher, I'm all ears but it works for now.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜