用于Rails 3数据确认属性的jQuery UI Dialog而不是alert() [英] jQuery UI Dialog instead of alert() for Rails 3 data-confirm attribute
问题描述
在Rails 3中,将:confirm参数传递给link_to将填充链接的data-confirm属性。这将在单击链接时引发JS alert()。
In Rails 3, passing a :confirm parameter to link_to will populate the data-confirm attribute of the link. This will induce a JS alert() when the link is clicked.
我正在使用rails jQuery UJS适配器( https ://github.com/rails/jquery-ujs )。来自rails.js的相关代码是:
I am using the rails jQuery UJS adapter (https://github.com/rails/jquery-ujs). The relevant code from rails.js is:
$('body').delegate('a[data-confirm], button[data-confirm], input[data-confirm]', 'click.rails', function () {
var el = $(this);
if (el.triggerAndReturn('confirm')) {
if (!confirm(el.attr('data-confirm'))) {
return false;
}
}
});
和
triggerAndReturn: function (name, data) {
var event = new $.Event(name);
this.trigger(event, data);
return event.result !== false;
}
我想知道如何将其修改为生成jQuery对话框(例如 jQuery UI对话框),允许用户确认或取消。
I would like to know how this could be modified to instead yield a jQuery dialog (e.g. the jQuery UI Dialog) allowing the user to confirm or cancel.
我对JavaScript的了解不足以实现这一目标。我目前的方法是简单地重写$('body')。delegate()函数来实例化灯箱。但是我想有一种比这更有效的方法。
My knowledge of JavaScript isn't sufficient to achieve this elegantly. My current approach would be to simply rewrite the $('body').delegate() function to instead instantiate a lightbox. However I imagine that there is a more effective approach than this.
推荐答案
我刚刚在Rails jquery-ujs中添加了一个外部API正是为了这种定制。您现在可以通过插入(并重写1行) $。rails.allowAction
函数来使rails.js使用自定义确认对话框。
I just added an external API to the Rails jquery-ujs for exactly this kind of customization. You can now make rails.js use a custom confirm dialog by plugging into (and re-writing 1 line of) the $.rails.allowAction
function.
请参阅我的文章, Rails jQuery UJS:Now Interactive ,有关示例的完整说明。
See my article, Rails jQuery UJS: Now Interactive, for a full explanation with examples.
编辑:从此提交,我将确认
对话框函数移动到 $。 rails
object,以便现在可以更轻松地修改或交换它。例如,
As of this commit, I moved the confirm
dialog function to the $.rails
object, so that it can be modified or swapped out even more easily now. E.g.
$.rails.confirm = function(message) { return myConfirmDialog(message); };
这篇关于用于Rails 3数据确认属性的jQuery UI Dialog而不是alert()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!