1

I start to learn CoffeeScript recently, and I faced with a problem as this. I want to write javascript :

TemplateManager.tmpl(this.template, this.modelJSON(), this.templateOptions()).done(
        function(rendered) { // something1
}).fail(function(ex) {
    // something2

});

Which way I can get it? I try rewrite that:

TemplateManager.tmpl @template, @modelJSON(), @templateOptions()
    .done (rendered) ->
       #something1
    .fail (ex) ->
       #something2

and I get:

TemplateManager.tmpl(this.template, this.modelJSON(), this.templateOptions().done(function(rendered) {

  }).fail(function(ex) {

  }));
0

2 Answers 2

3

Add parenthesis for tmpl and done methods

TemplateManager.tmpl( @template, @modelJSON(), @templateOptions() )
   .done( (rendered) -> 
        #something1 
    )
   .fail (ex) ->
        #something2

The solution isn't elegant, and I think others may give a better way in coffeescript

Updated

Base on comment, removing parenthesis for done. I've updated the code and I think this one is elegant

TemplateManager
   .tmpl(@template, @modelJSON(), @templateOptions())
   .done (rendered) -> 
        some
        code
        here 

   .fail (ex) ->
        another
        code
        here
Sign up to request clarification or add additional context in comments.

3 Comments

oops, sorry, i don't know why didn't wotk it,but it is worked,thanks
Parens needed only in .tmpl() call, .done() can be written without them.
@nl_0, I've checked it, yeah you're right. I'm updating my answer. Thanks.
2

Instead of making a mess of "I'm not using parentheses because they're optional" and tricky impenetrable indentation, just break things into little pieces, give the pieces names, and put them together simply:

done = (rendered) ->
    # something1
fail = (ex) ->
    # something2
TemplateManager.tmpl(@template, @modelJSON(), @templateOptions())
    .done(done)
    .fail(fail)

I have no idea what "something1" and "something2" are so I can't give them decent sensible names, consider done and fail as proof of concept names.

Just because a function can be anonymous doesn't mean is must be anonymous, just because some parentheses are optional doesn't mean that they must be left out.

Comments

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.