理由 :confirm 嵌套在 :data in link_to for Rails

Rationale for :confirm being nested within :data in link_to for Rails

link_to, :confirm is nested within :data, rather than being a direct key/value pair in options. What's the rationale for this? I suspect it's something to do with data attributes,但我不知道除此之外。

如果有人手动添加了一个包含 'data-confirm="Message" 的 link,这种方法是否允许他们收到确认提示?

早期 :confirm 选项没有嵌套在数据中,实现方式与今天不同。 In fact, when the option was passed, Rails injected an onclick confirm action directly in the HTML tag itself.

不过,在Rails3核心团队决定走低调的方向javascript。作为其中的一部分,过去导致 JavaScript 的所有事件直接注入到 HTML 标记中(包括例如远程提交表单或模拟 PUT/DELETE HTTP方法)被转换为基于 HTML 数据属性和使用 Rails UJS 文件正确附加到对象的事件的解决方案。

:confirm 选项随后被转换为数据属性。由于 Rails 标签助手或多或少在同一时间引入了使用 :data 选项设置任意数据属性的能力,我猜决定将 :confirm 嵌套在 :data 中并弃用 :confirm 选项主要是为了避免在各种助手中维护对单独选项的支持,其唯一范围是处理 confirm: "foo"data: { confirm: "foo" } 的转换。

有些情况下该选项仍然存在,例如 :remote 在表单中使用数据属性在后台设置表单的异步提交,但是在这种情况下 :remote 比简单的 "copy the value of this option to a data attribute" 执行更多任务,因此将它作为一个单独的选项保留是有意义的。

:confirm 的情况下,这将是不必要的开销。