1

如果他们在重新加载当前页面之前有任何未保存的电子邮件,我会尝试提示使用。未保存的电子邮件存储在称为未保存的哈希中。我在 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 返回字符串而不是函数?

4

1 回答 1

1

这与 CoffeeScript 无关。该属性window.onbeforeunload需要一个字符串或 void 从您的函数返回值,但如果unsaved.count <= 0您要返回其他内容:

return $("form.edit_email_template").first().submit;

因为返回值是非空的,它会尝试将submit函数转换为字符串。老实说,我不确定 CoffeeScript 生成该部分的原因,因为它似乎没有出现在原始脚本中。

也就是说,void如果您不想看到对话框,则应该返回:

if (unsaved.count > 0) {
    return 'your emails are not saved.';
} else if (window.onbeforeunload) {
    $("form.edit_email_template").first().submit();
}
于 2013-02-14T06:14:29.167 回答