簡體   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>

我有一個best_in_place表單,在這里對提交的數據進行一些后處理。 這是一系列的時間,我在保存之前將它們格式化為一致的格式。 保存后如何獲取更新的值以顯示在表單上?

我只能找到一種稍微有點怪異的方法。 首先,將表單助手上的display_as參數設置為與要顯示的實際屬性相同。

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

然后,為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'];
});

保存帖子並更新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中的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM