Ok, so I have this inside a form
<INPUT type="text" name="inputbox" value="">
<INPUT TYPE="button" NAME="button2" Value="Write" onClick="writeText(this.form)">
and I have the following generated javascript from coffeescript
(function() {
var writeText;
writeText = function(form) {
return form.inputbox.value = "ping";
};
}).call(this);
I need to know how do I call writeText?
as it is when I click the button nothing happens….if I remove the function(){}.call(this) wrapper it works.
I cannot remove the wrapper. I need to know how to call writeText with the .call(this) in place.
It’s a scoping issue, where Coffeescript wraps everything inside it’s own closure to avoid polluting the global namespace. There are two solutions;
Compile using coffeescript’s “–bare” option (which avoid putting the wrapper there in the first place).
Bind your coffeescript function to some object that the browser sees when running. Like this:
window.writeText = (form) ->
form.inputbox.value = "ping"
In the second option, your writeText function is bound to the window object in the browser, which is the implicit
thisobject when your script runs in the browser.