1

CoffeeScript wraps variables declared inside methods in an object literal.

So, this:

@Templates =
    get: (templateName) ->
        result: ''              # DECLARED HERE
        $.ajax(
            'Views/Templates/' + templateName + '.html',
            type: 'GET'
            dataType: 'html'
            success: (data) ->
                result = data   # ASSIGNED HERE
            async: false
        )
        return result           # RETURNED HERE

Becomes this:

(function() {

  this.Templates = {
    get: function(templateName) {
      ({
        result: ''                  //DECLARED IN AN OBJECT LITERAL - I DON'T WANT THIS
      });
      $.ajax('Views/Templates/' + templateName + '.html', {
        type: 'GET',
        dataType: 'html',
        success: function(data) {
          var result;               //DECLARED LOCAL TO THE CALLBACK - I DON'T WANT THIS
          return result = data;
        },
        async: false
      });
      return result;                //RETURNED HERE - UNASSIGNED
    }
  };

}).call(this);

But what I need, and that works for me, is this:

(function() {

  this.Templates = {
    get: function(templateName) {
      var result = ''               //DECLARED HERE
      $.ajax('Views/Templates/' + templateName + '.html', {
        type: 'GET',
        dataType: 'html',
        success: function(data) {
          return result = data;     //ASSIGNED HERE
        },
        async: false
      });
      return result;                //RETURNED HERE
    }
  };

}).call(this);

What am I doing wrong? How can I fix this?

3
  • That's not a closure it's an object literal and it's wrapped with parentheses so that it wouldn't be treated as a block Commented Feb 21, 2012 at 15:26
  • 1
    Synchronous GET? That's probably not a good idea. Commented Feb 21, 2012 at 15:37
  • I forgot to mention I am new to CoffeeScript and also Javascript. Thanks all for the answers and tips. Commented Feb 22, 2012 at 18:06

3 Answers 3

4

What you've called a closure isn't a closure (closures in JavaScript are always functions). It's an object literal wrapped in parentheses.

I'm not really au fait with CoffeeScript, but if you want result to be a local variable in the get function, I believe you want to change

result: ''

to

result = ''

The former is the object initializer format (hence it getting translated into an object literal), the latter is a variable assignment. Seems to be covered on the CoffeeScript site under lexical scoping.

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

Comments

2

By result: '' you declare a property of an object. What you needed was to declare a local variable by result = '':

get: (templateName) ->
    result = ''              # DECLARED HERE
    $.ajax(
        'Views/Templates/' + templateName + '.html',
        type: 'GET'
        dataType: 'html'
        success: (data) ->
            result = data   # ASSIGNED HERE
        async: false
    )
    return result           # RETURNED HERE

Comments

2

Why are you using a :? It's not being wrapped in a closure, you are just defining an anonymous object expression.

Just do this:

result = ''

That said, why are you doing a synchronous GET? That's very frowned upon and will make your site must less responsive.

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.