On Thu, Jan 22, 2009 at 12:33 AM, Allen Rohner <[email protected]> wrote:
>
> Chouser, how usable is clojurescript to generate extremely simple
> javascript calls? When creating HTML templates, I find myself using
> ugly string interpolation to generate the .js. I would really love it
> if I could do something like:
>
> (clojurescript/str (swfobject/embedSWF "open-flash-chart.swf"
> "my_chart", 500, 200, "9.0.0", false, {:data-file "/my/data"}))
>
> turns into:
>
> <script type="text/javascript">
> swfobject.embedSWF("open-flash-chart.swf", "my_chart", "550", "200",
> "9.0.0", "expressInstall.swf", {"data-file":"/my/data"});
> </script>
ClojureScript currently requires a patch to Clojure in order to work
(no breaking changes, though), and so the code is not in the regular
clojure-contrib path. That said, the API for that example would
currently be:
(require '[clojure.contrib.clojurescript :as cljs])
(cljs/formtojs '(.embedSWF swfobject "open-flash-chart.swf"
"my_chart" 500 200 "9.0.0" false {:data-file "/my/data"}))
There's a couple things to note there. The most important is that
JavaScript objects don't have packages or static members like Java
objects, so you use the instance-method call format a lot more often
in ClojureScript than in Clojure.
Another thing to note is commas are witespace. :-)
Anyway, the above returns this string (after adding whitespace by hand):
(function __user_fn_571(){
return ((clojure.JS.resolveVar("swfobject",user)).embedSWF(
"open-flash-chart.swf","my_chart",(500),(200),"9.0.0",false,
clojure.core.hash_map(clojure.core.keyword("","data-file"),"/my/data")))
}).apply(null,[])
The most important item to note here is that your {:data-file ...}
map is a Clojure hash-map, not a JavaScript Object. ClojureScript
doesn't currently do JSON-like translations for you. And unlike Java,
JavaScript doesn't have any way (that I know of) to provide a custom
object (like hash-map) that provides the same interface as the
builtin collection.
This is the kind of useful, practical, missing feature that probably
still abounds in ClojureScript. I'm not sure if there'd be a way to
use an existing clojure-to-json lib for this, or if something would
have to be ported. At least it could be written in ClojureScript
instead of JavaScrtipt!
You can try out ClojureScript and see what works (and likely uncover
bugs) with the browser-based REPL:
http://clojurescript.n01se.net/repl/
I haven't figured out yet why my clojure-created applets sometimes
fail on Mac, so that may not work in a Mac browser. I've made no
attempt to trim the size of either the applet or the .js code on that
page, so it may take a while to download and get everything started.
But once the applet rectangle has gone blank and the word
"ClojureScript" is on the page, you can try some expressions:
(alert "hi")
(-> document .body (.appendChild (.createTextNode document "Hello World")))
The most extensive example of ClojureScript code is, perhaps
unsurprisingly, the browser REPL itself:
http://clojurescript.n01se.net/repl/repl.cljs
--Chouser
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---