So, I’m probably missing something real basic, but brand new to js interop. How do I tell what a library packaged by cljsjs is named?
I’m working with PDF.js
(ns my.name.space
(:require [cljsjs.showdown]) ; note, no :as or :refer here
(defn convert-to-html [markdown]
;; note the syntax below: js/VarFromExternsFile.property
;; the dot on the end is the usual Clojure interop syntax: (Constructor. constructor-arg constructor-arg)
(let [converter (js/Showdown.converter.)]
;; methods you call will generally need to be called out as prototype values in the externs
(.makeHtml converter markdown)))
I saw this in the example, but wondering if I should just be guessing, or if there is a smarter way
http://stackoverflow.com/questions/9328551/how-to-use-pdf-js
it has the same name as if you'd just add a script take with the file to your HTML
In this case it isn’t a library I have previously worked with in JS
haven’t actually done much JS
so I just look for that in their documentation?
so js/pdf
if I read that stackoverflow answer right
and when they have a function that says new pdf()
I would call (js/pdf (pdf))
Most libraries export some global, jQuery exports $
and jQuery
, React exports React
and so on
(. js/pdf (pdf))
pdf()
= (js/pdf)
The exported globals can be accessed via the global Javascript namespace js/
awesome, thanks
if I want to chain things together, I can use the arrow macro right
so this gets me an object (.getDocument js/PDFJS "/onlisp.pdf")
to do something like this
PDFJS.getDocument('/files/tizenfordummies.pdf').then(function(pdfFile) {
var pageNumber = 1;
pdfFile.getPage(pageNumber).then(function(page) {
var scale = 1;
var viewport = page.getViewport(scale);
});
});
how does one translate callback hell to cljs?
this seems to 1) create a js object, 2) call .then on it, which takes another function, which is passed pdfFile, upon which you call .getPage, to return an object, on which you call .then, and pass a .getViewport
is this relevant? https://groups.google.com/forum/#!topic/clojure/PD3YTY9qoj0
(-> (.getDocument js/PDFJS "/onlisp.pdf")
(.then (fn callback1 [pdfFile] (.getPage pdfFile 1))))
so far so good
k, found a good example of interop using reagent and a js lib here https://github.com/timothypratley/leaderboardx/blob/a04d424f505bd5270da7a3905af8a6ccf73ae48f/src-cljs/algopop/leaderboardx/app/views/d3.cljs
@conaw: consider asking interop related questions in #C03S1L9DN, much more people there than here 🙂
yeah, hope the out load thinking wasn’t clutter
nah, I don't mind, just didn't see it until now 🙂