简体   繁体   English

best_in_place使用:display_as-如何更新 <input> 以及

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

I've got a best_in_place form where I am doing some post-processing of the data that gets submitted. 我有一个best_in_place表单,在这里对提交的数据进行一些后处理。 It's a series of times, and I'm formatting them into a consistent format before saving. 这是一系列的时间,我在保存之前将它们格式化为一致的格式。 How do I get the updated value to be shown on the form after saving? 保存后如何获取更新的值以显示在表单上?

I could only find a slightly hacky way of doing this. 我只能找到一种稍微有点怪异的方法。 First, set the display_as parameter on the form helper to be the same as the actual attribute being displayed. 首先,将表单助手上的display_as参数设置为与要显示的实际属性相同。

= best_in_place @post, :name, type: :input, :display_as => :name

Then, add a javascript handler for best_in_place:success 然后,为best_in_place:success添加一个javascript处理程序best_in_place:success

$(document).on('best_in_place:success', function(e, data) {
  $(this).data('bestInPlaceEditor').original_content = data['display_as'];
});

When the post is saved and the name attribute is updated, the new value will be returned as JSON: it will look like {"display_as": "updated post name"} . 保存帖子并更新name属性后,新值将以JSON的形式返回:看起来像{"display_as": "updated post name"} This javascript gets the value that is returned and sets it to the BIP editor's original_content attribute. 此javascript获取返回的值并将其设置为BIP编辑器的original_content属性。 When BIP renders the input (when you click on the post's name to edit it), the value of original_content is displayed. 当BIP呈现输入时(当您单击帖子的名称以对其进行编辑时),将显示original_content的值。

Tested using best_in_place 2.1.0. 使用best_in_place 2.1.0进行了测试。 The relevant code in best_in_place.js is: 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"]);
}

Here this.element is the that display when the field is not being edited. 此处的this.element是未编辑字段时显示的内容。 So, it's getting set with a different value to what original_content (which is what the gets when rendered) is. 因此,它被设置为与original_content (呈现时得到的内容)不同的值。 I'm not sure if this is intended or a bug in best_in_place. 我不确定这是故意的还是best_in_place中的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM