如果他们在重新加载当前页面之前有任何未保存的电子邮件,我会尝试提示使用。未保存的电子邮件存储在名为unsaved的哈希中。我在CoffeeScript中编写了脚本。
如果我使用此代码
window.onbeforeunload = () ->
return "Your emails are not saved. Click \"Save Email\" on any email to save them all. If you would like to discard your emails, just leave this page" if unsaved.count > 0
而不是提示我收到的消息:
function ( data, fn ) {
return arguments.length > 0 ?
this.on( name, null, data, fn ) :
this.trigger( name );
}
Are you sure you want to reload this page?
CoffeeScript翻译为:
window.onbeforeunload = function() {
if (unsaved.count > 0) {
return "Your emails are not saved. Click \"Save Email\" on any email to save them all. If you would like to discard your emails, just leave this page";
}
};
我应该如何让CoffeeScript返回字符串而不是函数?
答案 0 :(得分:1)
这与CoffeeScript无关。属性window.onbeforeunload
需要函数中的字符串或void返回值,但如果unsaved.count <= 0
您返回其他内容:
return $("form.edit_email_template").first().submit;
因为返回值是非void,它将尝试将submit
函数转换为字符串。说实话,我不确定为什么CoffeeScript会生成那个部分,因为它似乎没有出现在原始脚本中。
那就是说,如果你不想看到对话框,你应该返回void
:
if (unsaved.count > 0) {
return 'your emails are not saved.';
} else if (window.onbeforeunload) {
$("form.edit_email_template").first().submit();
}