【问题标题】:CoffeeScript onbeforeunload responding with function not stringCoffeeScript onbeforeunload 响应函数不是字符串
【发布时间】:2013-02-14 06:09:56
【问题描述】:

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

【问题讨论】:

  • 发生了其他事情,您的 CoffeeScript 中没有任何内容可以生成您的return $("form.edit_email_template").first().submit;。看看simplified version

标签: javascript browser coffeescript onbeforeunload


【解决方案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();
}

【讨论】:

  • @muistooshort 我的脑子一定是短路了,你说的没错:)
  • +1 因为这条评论,我差点在公共交通上笑出声^^ 顺便说一句,那是浦原吗?
猜你喜欢
  • 2015-04-11
  • 2023-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-20
  • 2018-08-02
  • 1970-01-01
  • 2023-03-19
相关资源
最近更新 更多