Rails-回调中的访问AJAX触发元素 [英] Rails - Access AJAX Triggering Element in Callback

查看:105
本文介绍了Rails-回调中的访问AJAX触发元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图,例如show.js.erb.而且我在另一个视图中有一个链接,例如

I have a view, say show.js.erb. And I have a link in another view such that

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

成功返回show.js.erb视图.从这个角度来看,我的问题是,有什么方法可以访问触发AJAX调用的元素,而不必求助于生成特定于单个元素的ID,例如... ...

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.

我尝试使用$(this),但这不起作用.

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

我想做些

$(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.

在public/javascripts/application.rb中:

In public/javascripts/application.rb:

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

然后在我的视图中查看弹出内容(例如app/views/mymodel/popup.js.erb):

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.

这篇关于Rails-回调中的访问AJAX触发元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆