Reputation: 115
In my Rails app I have some plain old JS:
function reorder(divid,url) {
jQuery(divid).sortable({
axis: 'y',
dropOnEmpty: false,
handle: '.drag',
cursor: 'crosshair',
items: 'li',
opacity: 0.4,
scroll: true,
update: function(){
jQuery.ajax({
type: 'post',
data: jQuery(divid).sortable('serialize'),
dataType: 'script',
url: url)
}
});
}
and it works when I call:
reorder("#pages","<%= changeorder_pages_path %>");
So I decided to convert my function to CoffeeScript which gave me this:
(function() {
var reorder;
reorder = function(divid, url) {
return jQuery("#pages").sortable({
axis: "y",
dropOnEmpty: false,
handle: ".drag",
cursor: "crosshair",
items: "li",
opacity: 0.4,
scroll: true,
update: function() {
return jQuery.ajax({
type: "post",
data: jQuery("#pages").sortable("serialize"),
dataType: "script",
complete: function(request) {
return jQuery("#pGESs").effect("highlight");
},
url: "/pages/changeorder"
});
}
});
};
}).call(this);
but my call doesn't work any more - I get the Firebug error:
reorder is not defined
So to my question - how do I call the function now it is CoffeeScripted?
I have read this: Calling a function by its name
But I have no idea what they are talking about. I have never used global=this and have no idea what it does or why I would want to use it.
I read this as well: http://elegantcode.com/2011/06/30/exploring-coffeescript-part-2-variables-and-functions/
As well as this: http://www.informit.com/articles/article.aspx?p=1834699
I realise that CoffeeScript is protecting me from global variables and making my code better - but I can't find an explanation as how to call functions.
I have played with the CoffeeScript web site and played with the cube function - so I should just be able to call the function name I think.
I'm asking the question because I have a gap in my knowledge - any help filling that gap will be much appreciated.
Upvotes: 3
Views: 221
Reputation: 28268
CoffeeScript doesn't create globals by default, and it seems as if you are trying to access a reorder
outside the file it was defined in.
However you can explicitly put something in the global namespace by writing
window.reorder = reorder
Upvotes: 2