Advertisement
sneilan

scriptjure ajax macro

Jul 18th, 2011
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; utility function to take a url, replace the $0..$n in the url with entities from params & call GET on it
  2. ; yeah, it does all that.
  3. ; FIRST USEFUL CLOJURE/SCRIPTJURE MACRO! w00000t!
  4. (defjsmacro access [url & params]
  5.   (var url (clj url))
  6.   (clj
  7.     (loop [l (range (count params)) blah (list) params `~params]
  8.       ;(swap! blah conj '(+ 4 5))
  9.       ;(println blah)
  10.       (if (not-empty l)
  11.         (recur
  12.           (rest l)
  13.           (cons `(set! ~(symbol "url") (.replace ~(symbol "url") ~(str "$" (first l)) ~(first params))) blah)
  14.           (rest params))
  15.         (cons 'do blah))))
  16.   (var jqXHR (.ajax $ {:type "GET" :url url}))
  17.   (set! jqXHR.success (fn [data]
  18.     (if (= data.status "failed")
  19.       (.log console data.msg)))))
  20.  
  21. ; call like this
  22. (js (access "/url/$0/$1/$2/" param0 param1 param3))
  23. ; It can have any number of parameters. It will take care of all that annoying AJAX crap :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement