【问题标题】:Load jquery opendialog asynchronously with ruby on rails using haml使用haml在rails上使用ruby异步加载jquery opendialog
【发布时间】:2013-07-15 03:29:24
【问题描述】:

我正在开发一个 Rails 网络应用程序。在我的一个观点中(在 index.html.haml)我有一个 jquery opendialog,我在其中列出了一些东西。 我的问题是我不想在加载索引页面但在 openclick 事件时加载它。 我将提供一些代码。我对 Rails、haml 和 jquery 还很陌生,因此欢迎任何帮助,提前谢谢。

问候。

#...stuff

#dialog creation and loading
.dialog{:id => "codes_#{stuff.id}", :title => t_s(:index, resource_class.to_s.downcase)}
  #dialog: haml code with the list loading that is done when the main page is loaded instead on being called at the onclick

#link that opens the dialog
= link_to t_s(:show, resource_class.to_s.downcase), '#', :onclick => "openDialog(#{stuff.id})", :title => t_s(:show, resource_class.to_s.downcase)+ " " +t_s(:index, resource_class.to_s.downcase)

#...stuff

这是一些生成的js:

$(document).ready(function(){
 $(".dialog").dialog({ width: 600, height: 400, autoOpen: false, modal: true, show: "blind", hide: "explode"
 });
}); 
function openDialog(id){ 
$("#codes_"+id).dialog( "open" ); return false; 
}

问候

【问题讨论】:

  • 你能分享你的javascript吗?
  • 问题是您在document.ready 上打开对话框,这就是为什么您的对话框会在文档准备就绪时加载。你想把它移到函数openDialog 里面。此外,我会添加编辑帖子的 JS 代码以获得更好的可读性。
  • 通过这样做,现在我加载页面时会显示对话框,而不是在我单击按钮之前隐藏

标签: javascript jquery ruby-on-rails ruby haml


【解决方案1】:

已经自己修好了,这里补充说明: 1)去掉了haml,只剩下de div,把haml放在一个partial中 2)在控制器中添加了一个动作:def dialog stuff = Stuff.find(params[:stuff_id]) render :partial => "partial_name",:locals => { :stuff => stuff }, :layout => false end

3)在javascript中:$("#div_id").empty();

$.ajax({ type: 'GET', url: '本例对话框中的操作的url', cache: false, data: { stuff_id: stuff_id }, success: function(html){ $("# div_id"+stuff_id).append(html); } });

希望这可以帮助其他人。

问候

【讨论】:

    猜你喜欢
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 2023-04-05
    • 2011-07-08
    • 1970-01-01
    • 2017-04-24
    相关资源
    最近更新 更多