Fork me on GitHub
#hoplon
<
2016-06-06
>
thedavidmeister13:06:16

@mhr i can help you get datascript working in hoplon if you want

thedavidmeister13:06:01

here’s a datascript database as a cell, with a query in cell=

thedavidmeister13:06:04

(def schema {})

(def conn (datascript.javelin/conn-cell schema))

(def not-seen (j/cell= (d/q '[:find ?e ?c :where  [?e :message/content ?c]
                                                  [?e :message/seen false]]
                            conn)))

thedavidmeister13:06:29

you can transact as normal

thedavidmeister13:06:33

(defn seen!
  [id]
  {:pre [(integer? id)]}
  (d/transact! conn [{:db/id id 
                      :message/seen true}]))

thedavidmeister13:06:23

this is all the “magic” you need

thedavidmeister13:06:26

(defn- conn-cell-from-db
  "Mimics datascript conn-from-db but builds a compatible javelin cell"
  [db]
  {:pre   [(d/db? db)]
   :post  [(d/conn? %) (j/cell? %)]}
  (j/cell db :meta { :listeners (atom {})}))

(defn conn-cell
  "Builds a fresh conn cell wrapping an empty db"
  ([]
   (conn-cell {}))
  ([schema]
   {:pre  [(map? schema)]
    :post [(d/conn? %) (j/cell? %) (= {} (-> % meta :listeners deref))]}
   (conn-cell-from-db (d/empty-db schema))))

thedavidmeister13:06:02

you can hook it into your dom something like this

thedavidmeister13:06:07

(h/defelem messages
  [_ _]
  (h/if-tpl (j/cell= (seq system-message.state/not-seen))
            (h/div
              :id "system-message"

              (h/for-tpl [[id content] system-message.state/not-seen]
                (h/div
                  :class "message"
                  (h/span
                    :class "message-text"
                    content)
                  (h/a
                    :class "message-close"
                    :click #(system-message.state/seen! @id)
                    :href "#"
                    " "))))))

thedavidmeister13:06:25

and that’s datascript + hoplon...

beatngu1315:06:24

Hey guys! I’m just getting started with this whole Hoplon/Javelin/Castra thing… 😁 I wonder what’s the preferred way to make HTTP requests to REST-ish endpoints and how to use returned data in JSON format. I’d be grateful for any help, advice, or internet resources. Thanks!

flyboarder15:06:18

@beatngu13: I assume you mean to non castra backends?

flyboarder15:06:39

you can make requests from the client or server to other endpoints

beatngu1315:06:18

Yep! Found the following question from Jun '14 and started looking at the mentioned examples: http://hoplon.discoursehosting.net/t/how-to-make-http-requests/151

beatngu1315:06:51

Great, thanks! I’ll have a look at http-kit.

flyboarder15:06:00

what I did in my case was use hoplon/castra in the usual way then from the server handle the request to the other servers, that let me use javelin/castra the usual way as well, where castra’s return value is placed into cells

flyboarder15:06:28

but you could also do it straight from the client and skip your server, depending on your needs

beatngu1316:06:10

Are there any best practices how to do this without a server at all? Making HTTP requests from the browser with Hoplon.

beatngu1316:06:14

I started to play around with cljs-http. What I really like is that it uses core.async channels, but since I’m new to Hoplon I’m not sure if those two things fit together.

flyboarder16:06:00

is that necessary? Personally (and probably because im missing something) I dont like using async channels

flyboarder16:06:16

if you just want a simple json request you can use jquery

flyboarder16:06:36

(js->clj (.. js/jQuery (get ")))

beatngu1316:06:10

Well, not necessary. But I do this as an university project as part of a series about JS alternatives and I wanted to show the CSP way… 😉

raywillig16:06:18

@beatngu13: I’m using jQuery getJSON to grab data from a http://Prismic.io content repository and deposit the results into a cell. The library is open source and you could have a look at it here https://github.com/phalanxsystems/coci-prismic-api

beatngu1316:06:48

Allright, I’ll go with jQuery. Thanks guys. 👍

leontalbot18:06:34

@raywillig Prismic seems interesting!

leontalbot18:06:47

@raywillig: Can you use it as a DB?

raywillig18:06:12

Their API doesn’t allow you to write to it. You can only do CUD from their writing room web app

micha18:06:45

haha C_UD

raywillig18:06:16

I ruminated on that acronym for a while

micha18:06:26

yeah it's a lot to chew on

raywillig18:06:50

we’re here all week folks

mhr19:06:32

I still haven't found the time to start playing around with clojure, cljs, and hoplon yet. I'm very interested in trying out figwheel. Does figwheel work well with hoplon? (Keep in mind I've not used figwheel nor hoplon so this may be a silly question.)

micha19:06:39

I don't see any reason why it wouldn't, but if you use the boot-hoplon task you'd need to use boot not lein

micha19:06:03

if you don't use the boot task you'd need to do what the task does yourself

micha19:06:29

boot has tasks to accomplish the same thing that figwheel does

mhr19:06:18

so I should look at it in terms of boot vs. figwheel to achieve that same live interactivity?

micha19:06:20

if you do boot -d seancorfield/boot-new new -t hoplon -n hellohoplon like it says in the getting started page, and then you do boot dev, you will have the same thing as what figwheel does

mhr19:06:36

okay fantastic, thank you!