SassAndCoffee compiles CoffeeScript with the 'bare' option. Can I control this? - coffeescript

If I install the SassAndCoffee.Core package from NuGet, and then ask SassAndCoffee to compile some CoffeeScript, it seems to pass the "bare" option to the CoffeeScript compiler -- i.e., it does not wrap my script in CoffeeScript's usual (function() { and }).call(this); bookends.
Is there a way I can make SassAndCoffee not use the "bare" option?
Note: this is in a desktop app, and I'm explicitly calling into SassAndCoffee's code -- this is not the magic rewriting that happens in an ASP.NET site.
More details: Here's my code to compile CoffeeScript using SassAndCoffee.
var sassCompiler = new CoffeeScriptCompiler();
var js = sassCompiler.Compile("alert 'Hello World'");
which results in the following (bare) output in the js variable:
alert('Hello World');
But if I write some straightforward JavaScript that calls the official CoffeeScript compiler with the default options, e.g. this HTML file (drop coffee-script.js into the same directory):
<script src="coffee-script.js"></script>
<script>
document.write("<pre>")
document.write(CoffeeScript.compile("alert 'Hello World'"))
document.write("</pre>")
</script>
I get the expected, wrapped JavaScript output:
(function() {
alert('Hello World');
}).call(this);
It looks like SassAndCoffee is calling CoffeeScript.compile(input, {bare: true}) instead of just CoffeeScript.compile(input).
I'd like to use SassAndCoffee.Core for its V8 support, but I want to be able to choose between default output and bare output. Short of rewriting SassAndCoffee's CoffeeScript compiler (which would kinda defeat the point of using SassAndCoffee), or manually prepending and appending the wrapper code (I'd feel dirty duplicating work that the compiler is supposed to do), is there any way I can get SassAndCoffee to output non-bare JavaScript?

If I'm reading this right, it seems to be an explicit decision by the developers:
https://github.com/xpaulbettsx/SassAndCoffee/blob/43300b7805db8b3dccf20cc71d1282ecfd8c76e1/SassAndCoffee.Core/CoffeeScript/coffee-script.js
That might also provide you the file you need to change to modify it to your needs.
As an alternative, in case you get stuck, I use Mindscape Web Workbench as a Visual Studio plugin that seems to do most of what it appears SassAndCoffee accomplishes for you.

Related

Scala template engine for creating js files

I want to create js files using a template engine with Scala. Is it possible with the popular templating engines for Scala, namely Play and Scalate? If possible, than what are the pros and cons for using either of them?
Just create view with .js ext, i.e.: app/views/myScript.scala.js and dummy content:
#(message: String)
alert("#message");
Then add an action into your controller:
def myScript = Action {
// use views.js... NOT views.html... !
Ok(views.js.myScript.render("Whoha! I'm dynamic JS in Scala :)"))
}
or in Java version:
public Result myScript(){
// use views.js... NOT views.html... !
return ok(views.js.myScript.render("Yey! I'm dynamic JS in Java :)"));
}
Add the route to this action:
GET /my-script controllers.Application.myScript()
So you can use this route directly:
<script src="/my-script"></script>
note, that Play should return valid Content-Type:text/javascript; charset=utf-8 in the response, anyway depending on version you are using it may be required to enforce this manually within your action (use browser's inspection tool to check the response type)
It really depends on what you want to achieve, i.e. how sophisticated your JavaScript code will be, but, unless it's something really small and simple, I'd suggest using the Scala.js. This way you basically will write some Scala code that will compile into JavaScript, and that compiled JavaScript you should be able to include into your Play application.
Advantages of writing Scala vs JavaScript should be pretty obvious (type safety, using lots of the existing Scala libraries). Disadvantage would be some delays for Scala -> JavaScript compilation, and also lack of the same seamless integration of Scala.js and Play, like Play has with its own templating engine. It's up to you to decide if the extra work to make these 2 work together worth it.

Fay: include another Fay file?

I have one Fay file which is the heart of my program, however I need some helpers for my logic, for instance a method to replace substrings. From what I understand, if I need such methods which are offered by many Haskell libraries from Hackage directly, I can't use those Haskell libraries, but I must copy-paste the code in my project. So it's what I did, I copy-pasted a "replace" function together with other helpers from the MissingH library in a new file in my project: Utils.hs.
That Utils.hs compiles without problems with Fay. Also I import it in my main Fay file and I get a JS file for the main project file without problems. However at runtime I get the following error:
ReferenceError: Utils$$36$ is not defined
I don't think that Fay will include the code from the helper file in my main JS file, so I'm including both JS files in the loading HTML. And to make even more sure that when I load the main file, that the utils file is loaded, I load it like that:
$.getScript("Utils.js", function(){
$.getScript("FayConfig.js");
});
But despite this I still get the error. I tried compiling the Utils.hs with "--library" but it didn't help.
So my question is, which setup do I need to achieve that the generated JS will find the helper functions that I put in another HS file, knowing that at compile-time, Fay (apparently) finds them without problems? Is there an example of such a setup online? Most of the Fay uses that I found have all the code in a single HS file, though they often use external Fay code from cabal, as with fay-jquery. In my case, setting up a cabal project just for these simple helpers would be overkill.
Which version of Fay are you using (fay --version)? It seems like you are using a version older than
0.16 where forgetting import Prelude wouldn't give any warnings, see this closed ticket. So upgrade fay and/or add import Prelude.
We're also considering renaming operators in the produced output to make error messages like these easier to understand.
You do not need to invoke fay several times, fay outputs all dependencies into the same js file. So there's no difference from using a cabal package in that regard.
Hope this helps, otherwise please give me a way to reproduce this.

Generalization and reuse of a part of template in scala (in playframework)?

Well, I am trying to generalize few code in my scala template of playframework. For example I have a template scala code, in many other scala templates. Something like:
<script type="text/javascript" src="somethig">
#form(routes.Application.post()) {
#inputText(Form("title"),args ='cols -> 80)
<input type="submit">
}
<script>...................</script>
I want to make a function that will hold the previous code and when I would call it, It will write that for me. In one word I want to reuse it in other templates. So how to do that?
I have tried to define function in my scala template but since it has javascript as well, it starts debugging js as scala! So I can't use it, now what can be done?
That function is called a tag in Play and is nothing else than mini-view, which you can call /include with params from other 'normal' views: check the Tags (they are just functions, right?) section in the doc.
For JS... afaik, there's still no good way for escape the JS in the Play views, so most secure way is avoiding placing JS directly in it. Instead import them as a common JS file in the document's head with <script src="...

How do I associate a CoffeeScript file with a view?

Just installed rails 3.1 rc1 and am trying to grok the best way to manage javascript with the new asset pipeline
By default all coffeescript is compiled into a single application.js file, this is a good thing.
Each seperate coffee script file is appended to the js file and wrapped in an anonymous function which is executed via the call method
A common scenario would be to use some jquery to turn various forms into ajax forms, update UI, etc...
Many of these scripts will be specific to a controller or action, I am trying to grok the 'conventional' way to handle this,
since everything is wrapped in an anonymous function how do I only execute just
the code for a particular controller / action, by default all of the anonymous functions are being executed
I did play around with some hacks where I load the controller and action name into js variables and then in
coffeescript check those to conditionally run code, I don't like that very much
my initial thought was that each coffee file would contain a js namespace/object and I would call the specific ones from the view,
going to spike this using the default_bare = true configuration
see How can I use option "--bare" in Rails 3.1 for CoffeeScript?
EDIT
Looking around some more: this looks like it might be the correct approach - "Can't find variable" error with Rails 3.1 and Coffeescript
There are two common approaches:
Make behavior conditional on the presence of a particular element. For instance, code to run a signup sheet should be prefaced with something like
if $('#signup').length > 0
Make behavior conditional on a class on the body element. You can set the body class using ERB. This is often desirable for stylesheets as well. The code would be something like
if $('body').hasClass 'user'
gistyle is a simple gem that helps you running action-specific javascript codes.
By following its setup, you set some data attributes in your body element, representing the current controller and action names. Then it will only call that action when the corresponding view is loaded.

Javadoc on CoffeeScript?

I'm new to CoffeeScript and seems that I can't find any document generator for CoffeeScript using Javadoc syntax. The only one I could find is available as a patch to the CoffeeScript compiler.
So, what do you use to generate documentation from Javadoc comment on CoffeeScript or how do you document your function arguments and return values?
So, JavaDoc syntax has never really caught on with JavaScript developers. There are those who use something like it—notably Google—but it's kind of at odds with JS, which doesn't have static typing and allows any number of arguments to any function.
If you want to create beautiful documentation with CoffeeScript, the standard is Docco (its home page is an excellent example). If you want to create JavaDoc-style comments for every function... well, you'll have to create them by hand, and escape them with backticks.
Alternatively, you could work in CoffeeScript until your code is release-ready, then document the resulting JavaScript.
Docco is great for prozedural coding style. If you want to document an API, coffeedoc is for you.
People looking forward to using javadoc style documentation in coffeescript can checkout codo ( http://netzpirat.github.com/codo/ ) which provides support for a subset of javadoc and automatically infers class names, function names and parameters from source code.
I'm using YUIDoc. I can add comments using a syntax similar to Javadoc to my classes, methods and events. The documentation gets output as html/css files and you can even customize the page layout.
Check this documentation example: http://yui.github.com/yuidoc/api/
PS: It relies on Node.JS and you need to install the package yuidocjs
npm install yuidocjs -g