1

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!

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help @alex. Being very new to CoffeeScript, I'm still not really versed in the syntax and when parentheses are necessary, implicit, or error-causing. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.