Fork me on GitHub
#cljsjs
<
2016-06-28
>
conaw09:06:41

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?

conaw09:06:48

I’m working with PDF.js

conaw09:06:50

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

conaw09:06:10

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

martinklepsch09:06:51

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

conaw09:06:27

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

conaw09:06:32

haven’t actually done much JS

conaw09:06:47

so I just look for that in their documentation?

martinklepsch09:06:48

so js/pdf if I read that stackoverflow answer right

conaw09:06:05

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

conaw09:06:16

I would call (js/pdf (pdf))

martinklepsch09:06:24

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

conaw09:06:28

(. js/pdf (pdf))

martinklepsch10:06:04

pdf() = (js/pdf)

martinklepsch10:06:29

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

conaw10:06:25

awesome, thanks

conaw10:06:14

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

conaw10:06:28

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

conaw10:06:54

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

conaw10:06:58

how does one translate callback hell to cljs?

conaw10:06:23

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

conaw10:06:29

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

conaw10:06:59

so far so good

martinklepsch10:06:04

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

conaw10:06:29

yeah, hope the out load thinking wasn’t clutter

martinklepsch10:06:43

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