使用:display_as -我如何更新<input>还有<span>

best_in_place using :display_as - how do I update the value of the <input> as well as the <span>

本文关键字:更新 input span 还有 display as 何更新 使用      更新时间:2023-09-26

我有一个best_in_place表单,我正在做一些提交的数据的后处理。这是一系列的时间,我在保存之前将它们格式化成一致的格式。我如何得到更新后的值显示在保存后的形式?

我只能找到一种略显粗糙的方法来做到这一点。首先,将表单帮助器上的display_as参数设置为与显示的实际属性相同。

= best_in_place @post, :name, type: :input, :display_as => :name
然后,为best_in_place:success添加一个javascript处理程序
$(document).on('best_in_place:success', function(e, data) {
  $(this).data('bestInPlaceEditor').original_content = data['display_as'];
});

当文章保存并更新name属性时,新值将以JSON形式返回:它将看起来像{"display_as": "updated post name"}。此javascript获取返回的值并将其设置为BIP编辑器的original_content属性。当BIP呈现输入时(当您单击帖子名称进行编辑时),将显示original_content的值。

使用best_in_place 2.1.0测试。best_in_place.js中的相关代码如下:

if (response !== null && response.hasOwnProperty("display_as")) {
  this.element.attr("data-original-content", this.element.text());
  this.original_content = this.element.text();
  this.element.html(response["display_as"]);
}

这里this.element是字段未被编辑时的显示。所以,它被设置为与original_content不同的值(这是渲染时得到的)。我不确定这是有意的还是best_in_place中的一个bug。