wrap-js : clojurescript - want some jquery plugins? managed? no problem.

164 views
Skip to first unread message

Dave Sann

unread,
Feb 9, 2012, 10:19:30 PM2/9/12
to clo...@googlegroups.com
I have what I think is a really nice method for using jquery and other js libs and their dependencies sanely in clojurescript.

(it will be even better if/when http://dev.clojure.org/jira/browse/CLJS-147 is resolved)


by using wrap-js (https://github.com/davesann/wrap-js) you can have clojurescript require manage the dependencies for you and insert the scripts into the page on bootstrap.js load. In the right order. no manual effort required.

briefly:

1. just jQuery

clojars [dsann/dsann-cljs-jquery "0.0.1"]    ( cljs-jquery was already taken)

(ns my client
  (:require [cljs-jquery.inline :as _jq]))

; use js/jQuery here - works - no need to include jquery in your html page

2. Even better: jQuery and plugin

clojars [cljs-jquery-sparkline "0.0.1"]   

(ns my client
  (:require [cljs-jquery-sparkline.inline :as _sl]))

; use js/jQuery and (.sparkline ($ ...)) here.  works - no need to include jquery or the plugin in your html page

3. And:

feel free to use jayq or any other library building apis on top of jquery.
just be sure to require  [cljs-jquery.inline :as _jq] before you make reference to jQuery

4. And!

Its really easy to wrap your own libs. 5 minutes and you are away.


see also



Cheers

Dave

Dave Sann

unread,
Feb 9, 2012, 10:36:03 PM2/9/12
to clo...@googlegroups.com
one note that just came to mind.

I can see a potential issue arising with the versions of wrapped js libs and compatibility - particularly for things like plugins.

I haven't done this yet. But it I think that it may be important to be be explicit about the version of the js lib you are wrapping.

so rather than naming the project 
"cljs-jquery" you would name it "cljs-jquery-1-7-1"

This will allow users/clients to choose which versions they want and manage compatibility.

D


Dave Sann

unread,
Feb 9, 2012, 10:55:27 PM2/9/12
to clo...@googlegroups.com
ok rules for wrapping.

1. include the version of the wrapped js lib in the name of the project/jar - so that users can choose which version they want.
2. use a generic namespace to inline the js lib - e.g jquery.inline - not jquery-1-7.inline  (allows you to switch versions)
3. do not include any wrapped libs as dependencies in your project.clj file - let the user be explicit about which lib the want to depend on in their project.clj. (if you don't do this, there will be no end of problems with multiple js libs and conflicts in the same page.)




Dave Sann

unread,
Feb 9, 2012, 11:39:45 PM2/9/12
to clo...@googlegroups.com
updated to follow my own rules and by way of example:

* [cljs-query-1-7-1             "0.0.1"]   for jquery     version 1.7.1
* [cljs-jquery-sparkline-1-6 "0.0.1"]   for sparkline version 1.6

Reply all
Reply to author
Forward
0 new messages