On 2011-12-04 21:17, Adam D. Ruppe wrote:
Jacob Carlborg Wrote:
I hide JavaScript behind CoffeeScript, makes it a bit more usable.
If you like the idea there, but want something a lot more conservative,
in my html.d (in here:
https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff
)
there's now a JavascriptMacroExpander class which pre-processes javascript.
The only build in function it provides right now is foreach() (just because I
find
it's lack to be the easiest thing to take care of...) but the macro system might
be useful too for certain things.
Again, it's just a preprocessor, so you drop stuff like this into the middle of
regular js code:
¤foreach(element; document.querySelectorAll("p")) {
element.style.color = "red";
}
converts to
var arsd_foreach_loop_temporary_2 = document.querySelectorAll("p");
for(var arsd_foreach_loop_counter_1 = 0; arsd_foreach_loop_counter_1<
arsd_foreach_loop_temporary_2.length; arsd_foreach_loop_counter_1++) {
var element =
arsd_foreach_loop_temporary_2[arsd_foreach_loop_counter_1];
element.style.color = "red";
}
which I realize is an ugly mess but just because I used long names for the
temporaries.
It: a) puts the iterable in it's own var, b) does a for loop over it, c) sets
up a local for
the iterated thing inside the loop and d) pastes your code in there.
(that symbol before foreach is the one I did to denote a macro. I put it
on a hotkey in my editor... but I wanted something that I'd never use in
normal code, to keep the preprocessor both dead simple and out of the
way.)
You can also define your own macros and variables in the JS code
or as delegates in D.
I actually wrote this macro system for css, but it works pretty well
here too. (html.d also includes a CssMacroExpander, which runs this
and a de-nesting function.)
Maybe you should take a look at SASS, it has if-statements, for-loops,
functions, mixins, variables and other useful features. Two syntaxes are
available, one which is a superset of CSS (SCSS) and one which doesn't
use semicolon and uses indentation for scopes (SASS).
http://sass-lang.com/
This is pretty new stuff, so I'll probably be adding more functions
as I use it more.
I think CoffeeScript works really well, it's been around a while and
it's the default way to handle JavaScript in Rails 3.1 and later
versions (SASS is the default way of handling CSS). Rails is what I'm
using for web development.
--
/Jacob Carlborg