CoffeeScript 中的解析错误 +骨干保存功能

发布于 2024-12-27 16:06:48 字数 657 浏览 2 评论 0原文

我正在尝试将 CoffeeScript 与 Backbone.js (通过 Brunch)结合使用,并且想在模型中实现保存功能,但它拒绝编译,我一直无法弄清楚原因。我需要将参数传递到保存函数并实现回调。

下面的代码在第二行给出了 Unexpected ':' 错误,但我不确定为什么:

class exports.Tag extends Backbone.model

  defaults:
    id: null
    tagId: null
    found: false
    location: "Not yet found..."
    finders: []
    pointValue: 0
    unlockCode: ""

  verifyCode = ( code ) ->
    @save { tagId: @get 'tagId', unlockCode: code },
      success: ( model, response ) ->
        @trigger 'verifySuccessful', response
      error: ( model, response ) ->
        @trigger 'verifyFailed', response

任何帮助表示赞赏...谢谢!

I'm trying to use CoffeeScript with Backbone.js (via Brunch) and I want to implement a save function in my model, but it refuses to compile and I have been unable to figure out why. I need to both pass parameters into the save function and implement callbacks.

The code below gives an Unexpected ':' error on the second line, but I'm not sure why:

class exports.Tag extends Backbone.model

  defaults:
    id: null
    tagId: null
    found: false
    location: "Not yet found..."
    finders: []
    pointValue: 0
    unlockCode: ""

  verifyCode = ( code ) ->
    @save { tagId: @get 'tagId', unlockCode: code },
      success: ( model, response ) ->
        @trigger 'verifySuccessful', response
      error: ( model, response ) ->
        @trigger 'verifyFailed', response

Any help appreciated...thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

已下线请稍等 2025-01-03 16:06:48

就在这里,你正在混淆解析器

@save { tagId: @get 'tagId', unlockCode: code },

是这个吗?

@save { tagId: @get('tagId'), unlockCode: code },

还是这个?

@save { tagId: @get('tagId', unlockCode: code) },

两者都可以修复错误,但它们显然意味着不同的事情。

我想补充一点,隐式括号确实很棒,但只有在最简单的情况下才能利用它。我通常使用的规则是省略一行中第一个函数调用的括号,并将它们用于所有其他函数。这条规则可以让你完全摆脱这个问题。

It's here, you are confusing the parser

@save { tagId: @get 'tagId', unlockCode: code },

Is that this?

@save { tagId: @get('tagId'), unlockCode: code },

Or this?

@save { tagId: @get('tagId', unlockCode: code) },

Either will fix the error, but they obviously mean different things.

And might I add, implicit parens are indeed awesome, but take advantage of that awesome only in the most simple of circumstances. A rule I usually use is that omit parens for the first function invocation in a line, and use them for all others. This rule would have saved you from this issue entirely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文