【发布时间】:2015-05-18 22:17:34
【问题描述】:
我的 Rails 应用程序包含一个类似 Excel 的可编辑表格,始终使用 best_in_place 字段。它们在页面加载时工作得很好,在用户编辑 best_in_place 字段后,我在ajax:success 上调用咖啡脚本函数没有问题。
由于这是一个类似 Excel 的表格,因此用户可以单击链接以向表格中添加新的占位符行。我的 create.js.erb 文件在不重新加载页面的情况下添加了行。新行有几个具有 best_in_place 功能的表格单元格,更新它们会更新数据库,到目前为止一切顺利。
问题在于,当用户随后在 new 行之一中编辑 best_in_place 字段时,ajax:success 不会触发,因此我的咖啡脚本函数不会启动。
我已确认新表格单元格与现有表格单元格具有完全相同的类和数据属性(在类名上读取ajax:success)。
问题 - 在这种情况下,如果 ajax:success 未被识别,我该如何启动我的咖啡脚本功能?
里程碑控制器,创建和更新操作
def create
@payment_schedule = PaymentSchedule.find(params[:id])
build_default_milestone # this builds a placeholder milestone
if @milestone.save
increment_milestone_positions # this relates to a sortable table
respond_to do |format|
format.js
format.html { redirect_to :back }
end
else
respond_to do |format|
format.js
format.html { redirect_to :back }
end
end
end
def update
@milestone = Milestone.find(params[:id])
respond_to do |format|
if @milestone.update(milestone_params)
update_milestone_amounts
format.html { redirect_to(:back) }
format.json { respond_with_bip(@milestone) }
else
format.html { redirect_to(:back) }
format.json { respond_with_bip(:back) }
end
end
end
创建.js.erb
$('#milestone-body').prepend('<%= escape_javascript(render partial: 'row_add', locals: { milestone: @milestone }) %>');
咖啡脚本功能
update-percent 是所讨论的 best_in_place 字段的类名。此函数对现有行完美触发,而不是新行。
$('.update-percent').on 'ajax:success', (event, data, status, xhr) ->
...
关于如何解决这个问题的任何建议?
【问题讨论】:
-
“类似 Excel 的可编辑表格” = 电子表格
-
你可以检查 jQuerys
ajaxComplete()fn 和字母检查它是否成功。 Docs
标签: ruby-on-rails ajax coffeescript best-in-place