This coffee script code
class TestCoffee
constructor: (@saludo) ->
helloCoffee: ->
alert @saludo+" Coffee v7"
is generating the following javascript with mindscape web workbench 2.0.332.18684
(function() {
var TestCoffee;
TestCoffee = (function() {
function TestCoffee(saludo) {
this.saludo = saludo;
}
TestCoffee.prototype.helloCoffee = function() {
return alert(this.saludo + " Coffee v7");
};
return TestCoffee;
})();
}).call(this);
How should I use this code inside my asp.net mvc 3 view?
I'm importing the js code with
<script src="@Url.Content("~/Scripts/helloCoffe.js")" type="text/javascript"></script>
and trying to use with
<script type="text/javascript">
$(document).ready(function () {
var coffee;
coffee = TestCoffee("Jelouuuu");
coffee.helloCoffee();
});
</script>
I got a TestCoffee undefined error
So, how should I use it?
Thanks In Advance!
This is by design:
Although suppressed within this documentation for clarity, all CoffeeScript output is wrapped in an anonymous function: (function(){ ... })(); This safety wrapper, combined with the automatic generation of the var keyword, make it exceedingly difficult to pollute the global namespace by accident.
If you'd like to create top-level variables for other scripts to use, attach them as properties on window, or on the exports object in CommonJS. The existential operator (covered below), gives you a reliable way to figure out where to add them; if you're targeting both CommonJS and the browser: exports ? this
http://jashkenas.github.com/coffee-script/
I normally use jQuery $.extend to push a function into the jquery namespace for other scripts to use.
IE:
file1.coffee
$ ->
$.extend
ErrorAlert: (text) ->
simpleGritter $("#SuccessGritter"), text
return false
file2.coffee
$ ->
$("#right").ajaxError (event, request, settings) ->
$.ErrorAlert("Some kind of bad thing happened at: " + settings.url)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With