git clone 'https://github.com/ibdknox/jayq.git'

(ql:quickload :ibdknox.jayq)


Clojars Project

A jQuery wrapper for ClojureScript.

Wait a second.. I thought we weren't supposed to use jQuery?

When ClojureScript first came out, the prevailing wisdom was that since jQuery wasn't compatible with the Google Closure Compiler, we shouldn't be using it. So I set off to work on Pinot, a library that wrapped the goog.* APIs. The journey was painful and frustrating, and before long I realized I was essentially rewriting parts of jQuery for no gain. Instead of doing that, I figured my time would be better spent building on top of the most battle hardened JS library out there. Before I started down this path though, I wanted to make sure that I had answers to all the arguments against using it. They were as follows:

I'll tackle each of these in order.

In terms of it not being able to be compiled, we shouldn't be packaging jquery with our apps anyways. Virtually the entire web is built on $ and that means nearly every computer connected to the internet already has a google CDN'd version of jquery on their machine. That means there's no extra weight and no reason to compile it. This also addresses the first part of the second one - there's nothing more to download.

While it's true that if we used jQuery directly, all method calls would be left alone and could not be replaced with a minified name. With a wrapper, however, that happens exactly once per method and all occurences of the wrapper will be munged. This means that at most we're talking about a difference on the magnitude of bytes. If you need to optimize for size at that level, you shouldn't be using CLJS anyways.

Lastly, there's the argument that it won't lead to idiomatic usage. That's likely true if we use jQuery directly, but I'm not sure I really believe that's a valid argument. The same goes for Clojure if we use Java libraries directly all over the place. Wrappers, however, allow us to utilize all the functionality provided by these libraries, but still create Clojure idioms over top of them. There's no reason to ignore the most solid base out there, when we can just build greater abstractions on top of it. Do I think jQuery is the pinnacle of the client side web? Not at all, but I do believe it provides a great foundation for us to build exactly that.


Add the following to your project.clj

[jayq "2.5.4"]

Note: If you are upgrading to a major version be sure to check the changelog for breaking changes.


jayq.core Source

Most of the API in jayq.core maps 1:1 with jQuery functions.

(ns myapp
  (:use [jayq.core :only [$ css html]]))

(def $interface ($ :#interface))

(-> $interface
  (css {:background "blue"})
  (html "Loading!"))

jayq.core/ajax clojure & edn mime types support

Requests with :contentType option matching text/clojure text/edn application/clojure application/edn (as string or keyword) will have the :data turned into its string representation (via pr-str)

Responses with text/clojure text/edn application/clojure application/edn mime types are read as clojure data before being passed to callbacks. The same applies if the dataType option is set to :edn or :clojure.

jayq.util Source

jayq.macros Source

(let-ajax [a {:url "http://localhost:8000/1.json"
              :dataType :json}
           b  {:dataType :json :url "http://localhost:8000/2.json"}]
       (merge a b))
    [a (jq/ajax "http://localhost:8000/1.json")
     :let [foo "bar"]
     :when (= (concat a foo) "foobar")
     b (jq/ajax "http://localhost:8000/2.json")]
(merge a b foo))

Error handling in let-ajax and let-deferred forms should be done using jq.core/fail or the :error key on the request map.




If you're using advanced Clojurescript compilation you'll need to reference a jQuery externs file.

You can find externs files from the closure-compiler repository for a specific jQuery version.

Add this to your compilation options (assuming that your put the externs file in ./externs/):

    :optimizations :advanced
    :externs ["externs/jquery.js"]

Without this, you will see errors like Object ... has no method XX. See http://lukevanderhart.com/2011/09/30/using-javascript-and-clojurescript.html for more on externs.


Copyright (C) 2011 Chris Granger

Distributed under the Eclipse Public License, the same as Clojure.