Need help formatting CoffeeScript using the plugin in Visual Studios 2012 -
what's exact cofeescript produce following javascript:
var addartist= function() { var adddiv, artistval; adddiv = $("#artistname"); artistval = $("#artistinput").val(); $(" <div id=\"artistname2\"><label>" + artistval + "</label> /div>").appendto(adddiv); return false; };
i tried using http://js2coffee.org/ convert cofee
which produces:
addartist = -> adddiv = undefined artistval = undefined adddiv = $("#artistname") artistval = $("#artistinput").val() $(" <div id=\"artistname2\"><label>" + artistval + "</label> /div>").appendto adddiv false
when paste visual studios uses cofeescript plugin ouput is:
(function() { var addartist; addartist = function() { var adddiv, artistval; adddiv = $("#artistname"); artistval = $("#artistinput").val(); $(" <div id=\"artistname2\"><label>" + artistval + "</label> /div>").appendto(adddiv); return false; }; }).call(this);
so coffeescript plugin wrapping script function the addartist variable never leaves scope of inner function.
which results in "uncaught reference error addartist" on web page.
the question using visual studios plugin, exact cofee script produce below javascript works
var addartist= function() { var adddiv, artistval; adddiv = $("#artistname"); artistval = $("#artistinput").val(); $(" <div id=\"artistname2\"><label>" + artistval + "</label> /div>").appendto(adddiv); return false; };
coffeescript wrap generated javascript in function wrapper keep things accidentally leaking global scope unless compile --bare
:
-b, --bare
compile javascript without top-level function safety wrapper.
if want addartist
in global scope have explicitly put there:
@addartist = -> adddiv = $("#artistname") artistval = $("#artistinput").val() $("<div id='artistname2'><label>#{artistval}</label></div>").appendto adddiv false
or:
window.addartist = -> ...
if you're worrying browser environment. i've cleaned of other things js2coffee did code.
js2coffee has other bugs i'd recommend either leave working javascript javascript or learn coffeescript , conversion manually. coffeescript ends javascript anyway there's no need convert working code. , if conversion manually, you'll end understanding coffeescript better , you'll have more idiomatic coffeescript code you'll translating intent , function rather tokens.
Comments
Post a Comment