best_in_place运作良好,但我想使用fontawesome图标作为可选的“确定”按钮,而不是字符串。如何在'<i class="icon-ok"></i>'.html_safe
哈希中加入:ok_button
语法?
= best_in_place @book, :default_price_amount, :html_attrs => {:class => 'medium_no_dropdown'}, :ok_button => "OK"
这是一个老问题,现在使用:ok_button_class选项在best_in_place gem中支持所需的功能。用法是这样的:
<%= best_in_place @post, :title, :ok_button => "Submit", :ok_button_class => "btn post-title" %>
有一个解决方案,但不完全是为ok_button添加样式。如果您不介意使用unicode字形,可以尝试:
= best_in_place @book, :default_price_amount, :html_attrs => {:class => 'medium_no_dropdown'}, :ok_button => "✓".html_safe
The table with all the unicode字符可能是您对另一个变体的参考。
ok_button的真实样式的问题是散列只接受数据属性来定义按钮。可能在下一版本的BIP中,这将得到改进。
在源代码中,创建按钮的位置(best_in_place.js):
if(this.okButton) {
output.append(
jQuery(document.createElement('input'))
.attr('type', 'submit')
.attr('value', this.okButton)
)
}
'value'是我们传递的哈希值。如果有一种方法可以引用由真棒字体(
for icon-ok)定义的字形代码,那就太漂亮了。
由于我花了几个小时做同样的事情,我发现我们可以覆盖this function的原型来创建<button>
而不是<input type="button">
。然而,activateForm
function只等待来自input[type="button"]
的点击事件,因为我无法覆盖它,所以我尝试另一种(有点脏)的方式 - 它的工作原理。
在另一个js标记/文件中覆盖此脚本
BestInPlaceEditor.prototype.placeButtons = function (output, field){
'use strict'
// the ok button isn't changed
if (field.okButton) {
output.append(
jQuery('<button>').html(field.okButton).attr({
type: 'submit',
class: field.okButtonClass
})
)
}
if (field.cancelButton) {
// create new cancel "<button>"
var $resetBtn = jQuery('<button>').html(field.cancelButton).attr({
type: 'reset',
class: field.cancelButtonClass
}),
// and traditional cancel '<input type="button">', but this should be hidden
$_resetBtn = jQuery('<input>').val(field.cancelButton).css({ display: 'none' })
.attr({
type: 'button',
class: '__real-btn-close-best_in_place'
});
// and bind event to 'trigger' click traditional button when the new <button> is clicked
$resetBtn.bind('click', function (event) {
$(event.currentTarget).parents('form.form_in_place').find('input.__real-btn-close-best_in_place').trigger('click');
event.stopPropagation(); // << also neccessary
});
// append both
output.append($_resetBtn).append($resetBtn);
}
}
}