【问题标题】:Rails: a simple remote: true call does not generate js responseRails:一个简单的远程:真正的调用不会产生js响应
【发布时间】:2014-06-15 01:05:23
【问题描述】:

我正在使用 iframe 在我的 rails 应用程序中显示托管在同一域中的一些静态 html 文件。我正在将具有远程属性的链接添加到 iframe 内的元素,希望进行就地 ajax 调用以创建新的嵌套资源,如下所示:

----project.js----
var f = $( ".myFrame" );
f.load(function(){
        $(this).find('.jd-details-title').append('<a data-remote="true" href="/projects/10/issues/new" id="new_link">New Issue</a>');
});
----javadocs.html.erb----
<iframe width="100%"
                  height="1200"
                  frameborder="0"
                  scrolling="auto"
                  class='myFrame'
                  src= <%= /path/to/static/htmls/ %> >
          </iframe>

当我点击 iframe 内的这个链接时,iframe 被重定向到新的问题 html 页面,但是当它移到 iframe 之外时,它会神奇地调用新问题的 js 版本。

我在这里缺少什么?如何修改此代码以获得相同的行为?

【问题讨论】:

    标签: javascript jquery html ruby-on-rails iframe


    【解决方案1】:

    remote: true 由 Rails 上名为 jquery_ujs 的 JavaScript 库控制(您可以在 application.js 中看到)。因为你的 iframe 页面不包含这个库,所以它无法理解remote: true(记住 iframe 里面的页面是和父页面完全隔离的)。

    在这种情况下,我认为您必须手动处理 iframe 中的链接。例如:

    $("link_id_inside_iframe").click(function(e){
       e.preventDefault();
       var link = $(this).attr("href");
       $.getScript(link);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-22
      • 2012-12-03
      • 2012-07-16
      • 1970-01-01
      相关资源
      最近更新 更多