cljsjs

conaw 2016-06-28T09:56:41.000352Z

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?

conaw 2016-06-28T09:56:48.000353Z

I’m working with PDF.js

conaw 2016-06-28T09:56:50.000354Z

(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)))

conaw 2016-06-28T09:57:10.000355Z

I saw this in the example, but wondering if I should just be guessing, or if there is a smarter way

martinklepsch 2016-06-28T09:57:51.000359Z

it has the same name as if you'd just add a script take with the file to your HTML

conaw 2016-06-28T09:58:27.000361Z

In this case it isn’t a library I have previously worked with in JS

conaw 2016-06-28T09:58:32.000362Z

haven’t actually done much JS

conaw 2016-06-28T09:58:47.000363Z

so I just look for that in their documentation?

martinklepsch 2016-06-28T09:58:48.000364Z

so js/pdf if I read that stackoverflow answer right

conaw 2016-06-28T09:59:05.000365Z

and when they have a function that says new pdf()

conaw 2016-06-28T09:59:16.000366Z

I would call (js/pdf (pdf))

martinklepsch 2016-06-28T09:59:24.000367Z

Most libraries export some global, jQuery exports $ and jQuery, React exports React and so on

conaw 2016-06-28T09:59:28.000368Z

(. js/pdf (pdf))

martinklepsch 2016-06-28T10:00:04.000369Z

pdf() = (js/pdf)

martinklepsch 2016-06-28T10:00:29.000370Z

The exported globals can be accessed via the global Javascript namespace js/

conaw 2016-06-28T10:10:25.000371Z

awesome, thanks

conaw 2016-06-28T10:11:14.000372Z

if I want to chain things together, I can use the arrow macro right

conaw 2016-06-28T10:11:28.000373Z

so this gets me an object (.getDocument js/PDFJS "/onlisp.pdf")

conaw 2016-06-28T10:11:54.000374Z

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);
    });
});

conaw 2016-06-28T10:13:58.000375Z

how does one translate callback hell to cljs?

conaw 2016-06-28T10:17:23.000376Z

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

conaw 2016-06-28T10:17:39.000377Z

is this relevant? https://groups.google.com/forum/#!topic/clojure/PD3YTY9qoj0

conaw 2016-06-28T10:20:29.000378Z

(-> (.getDocument js/PDFJS "/onlisp.pdf")
     (.then  (fn callback1 [pdfFile] (.getPage pdfFile 1))))

conaw 2016-06-28T10:20:59.000379Z

so far so good

conaw 2016-06-28T10:27:27.000380Z

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

martinklepsch 2016-06-28T10:31:04.000382Z

@conaw: consider asking interop related questions in #C03S1L9DN, much more people there than here 🙂

conaw 2016-06-28T10:31:29.000383Z

yeah, hope the out load thinking wasn’t clutter

martinklepsch 2016-06-28T10:31:43.000384Z

nah, I don't mind, just didn't see it until now 🙂