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?