keechma

Keechma stack. Mention @U050986L9 or @U2J1PHYNM if you have any questions
mihaelkonjevic 2016-04-25T04:42:00.000009Z

@roberto: you can add meta data on collections and named items

roberto 2016-04-25T12:40:14.000011Z

thanks, it is what I ended doing.

roberto 2016-04-25T12:40:38.000012Z

another question, in the Place My Order example, specifically this https://github.com/keechma/keechma-place-my-order/blob/66cd3138897f72f9e683dae2e562610c55cd8984/client/src/client/controllers/restaurant.cljs#L24

roberto 2016-04-25T12:41:15.000014Z

the :recordings :current entity is being attached some meta data. I noticed this overwrites the meta data for :recordings :list.

roberto 2016-04-25T12:41:20.000015Z

is that the expected behavior?

mihaelkonjevic 2016-04-25T12:44:11.000016Z

that shouldn’t be happening. I’ll investigate and get back to you

roberto 2016-04-25T12:46:56.000017Z

oh, I think I know why, because the entire app-db-atom is being reset!

roberto 2016-04-25T12:47:17.000020Z

is there a way to just update it instead of resetting?

mihaelkonjevic 2016-04-25T12:47:58.000021Z

but ti should return the whole new version of atom with only the (relevant) part of the data changed.

roberto 2016-04-25T12:48:47.000022Z

hmmm, for me it is changing the entire atom, deleting the :list and only :current is present

mihaelkonjevic 2016-04-25T12:50:05.000023Z

it deletes only the meta data or the data too?

mihaelkonjevic 2016-04-25T12:50:25.000024Z

also how are you retrieving the meta data in your ui component?

roberto 2016-04-25T12:50:49.000025Z

both

roberto 2016-04-25T12:51:28.000026Z

I’m using get-collection-meta

mihaelkonjevic 2016-04-25T12:51:34.000027Z

yeah, that’s the right way

roberto 2016-04-25T12:51:47.000028Z

but only for troubleshooting, was wondering why my list disappeared when I selected an item

mihaelkonjevic 2016-04-25T12:52:14.000029Z

that definitely shouldn’t be happening

mihaelkonjevic 2016-04-25T12:53:15.000030Z

you are seeing this behavior only in your app or in place-my-order app too?

roberto 2016-04-25T12:53:52.000031Z

i haven’t tried place-my-order, planning on doing that today. Seeing it on my app right now, which is tightly based on place-my-order

mihaelkonjevic 2016-04-25T12:54:08.000032Z

can you paste the code that shows that behavior?

roberto 2016-04-25T12:54:15.000033Z

I have a list on the left side, and the selected item is displayed on the right side

roberto 2016-04-25T12:54:28.000034Z

1 second, they are two controllers

roberto 2016-04-25T12:55:21.000035Z

this is the function in the controller for displaying the individual item:

(defn update! [app-db-atom updater]
  (reset! app-db-atom (updater @app-db-atom)))


(defn load-recording
  [app-db-atom slug]
  (update! app-db-atom
           #(edb/insert-named-item % :recordings :current {} {:is-loading? true}))
  (let [recording (edb/get-item-by-id @app-db-atom :recordings (js/parseInt slug))]
    (when-not recording
      (go
        (let [req (<! (http/get (str "/api/recordings/" slug)))
              meta {:is-loading? false}
              [success data] (unpack-req req)]
          (update! app-db-atom
                   #(edb/insert-named-item % :recordings :current data meta)))))))

roberto 2016-04-25T12:55:46.000036Z

this is the what loads the list:

(defn load-collection
  [entity list app-db-atom req]
  (do
    (reset! app-db-atom
            (edb/insert-collection @app-db-atom entity list [] {:is-loading? true})))
  (go
    (let [[is-success? body] (unpack-req (<! req))
          data               (if is-success? (:recordings body) [])
          max-pages         (.ceil js/Math (/ (:total body) page-size))
          meta               (if is-success?
                               {:is-loading?  false
                                :current-page (:current-page body)
                                :total        (:total body)
                                :max-pages    max-pages
                                :next-page    (next-page (:total body) max-pages (:current-page body))}
                               {})]
      (reset! app-db-atom
              (edb/insert-collection @app-db-atom entity list data meta)))))

(def load-recordings (partial load-collection :recordings :list))

roberto 2016-04-25T12:56:12.000037Z

this is the list controller:

(defrecord Controller []
  controller/IController
  (params [_ route]
    ;;Only run this controller when the route is home or "recordings".
    (let [route-data (:data route)]
      (when (or (and (= (:section route-data) "recordings")
                     (:page route-data))
                (= (:section route-data) "home"))
        route-data)))

  (start [this params app-db]
    (if (= "home" (:section params))
      (controller/redirect this {:section "recordings" :page 1})
      (when (:page params)
        (controller/execute this :load-recordings params)))
    app-db)

  (handler [_ app-db-atom in-chan out-chan]
    (go (loop []
          (let [[command args] (<! in-chan)]
            (case command
              :load-recordings (let [url (str "/api/recordings?page=" (:page args))
                                     req (http/get url)]
                                 (load-recordings app-db-atom req))
              (get-collection app-db-atom))
            (when command (recur))))))

  (stop [_ _ app-db]
    (edb/remove-collection app-db :recordings :list)))

roberto 2016-04-25T12:56:33.000038Z

the default case is just this:

roberto 2016-04-25T12:56:36.000039Z

(defn get-collection
  [app-db-atom]
  (prn "METAS: " (edb/get-collection-meta @app-db-atom :recordings :list))
  (prn "COLLECTION: " (edb/get-collection @app-db-atom :recordings))
  (edb/get-collection @app-db-atom :recordings :list))

roberto 2016-04-25T12:56:51.000040Z

which i placed there because my list kept disappearing

mihaelkonjevic 2016-04-25T12:57:05.000041Z

this line won’t return anything (prn "COLLECTION: " (edb/get-collection @app-db-atom :recordings))

mihaelkonjevic 2016-04-25T12:57:16.000042Z

because you need to give the name of the collection

mihaelkonjevic 2016-04-25T12:57:31.000044Z

if :recordings is the entity name

mihaelkonjevic 2016-04-25T12:57:42.000045Z

you need to pass the :list as the collection name too

roberto 2016-04-25T12:57:51.000046Z

even if I give the name of the collection, it returns an empty list

roberto 2016-04-25T12:58:18.000047Z

this is the controller for the selected item:

(defrecord Controller []
  controller/IController
  (params [this route]
    (let [route-data (:data route)
          page       (:section route-data)
          slug       (:slug route-data)]
      (when (and (= page "recordings")
                 (not (nil? slug)))
        route)))

  (start [this params app-db]
    (let [slug (get-in params [:data :slug])]
      (controller/execute this :current-recording slug))
    app-db)

  (handler [this app-db-atom in-chan out-chan]
    (go (loop []
          (let [[command args] (<! in-chan)]
            (case command
              :current-recording (load-recording app-db-atom args)
              :backup-to-vimeo (backup-to-vimeo app-db-atom args)
              nil)
            (when command (recur)))))))

roberto 2016-04-25T13:02:18.000048Z

for what it’s worth, I think it is because of the update! function

roberto 2016-04-25T13:02:31.000049Z

it is using reset! which, well, will replace the entire atom

mihaelkonjevic 2016-04-25T13:02:56.000050Z

yeah, but it should update just the part of the internal map. I’ll definitely have to check what’s going on

mihaelkonjevic 2016-04-25T13:03:29.000051Z

btw you can shorten the code inside the handler functions by using the dispatcher function https://github.com/keechma/keechma-todomvc/blob/master/src/keechma_todomvc/controllers/todos.cljs#L28

roberto 2016-04-25T13:03:56.000053Z

ah, nice

roberto 2016-04-25T13:04:45.000054Z

ah, and the update! function in that example is using swap! instead of reset!

roberto 2016-04-25T13:04:47.000055Z

will try that

mihaelkonjevic 2016-04-25T13:05:33.000056Z

I’m not using EDB in that app

mihaelkonjevic 2016-04-25T13:05:35.000057Z

that’s why

roberto 2016-04-25T13:06:06.000058Z

ah, ok

mihaelkonjevic 2016-04-25T13:06:22.000059Z

EDB has it’s own internal structure which is why I’m replacing the whole atom

roberto 2016-04-25T13:10:29.000060Z

I’m probably understanding something wrong, isn’t app-db-atom completely replaced with a new value by reset!? Or is this not a typical clojure atom?

mihaelkonjevic 2016-04-25T13:13:06.000062Z

it is, but the map inside should retain the old data.

mihaelkonjevic 2016-04-25T13:13:30.000063Z

it seems that edb is removing some data it shouldn’t

roberto 2016-04-25T13:15:16.000064Z

yeah, if I reach into the atom manually, I can see the list there

roberto 2016-04-25T13:15:29.000065Z

(:entity-db @app-db-atom)

mihaelkonjevic 2016-04-25T14:01:50.000066Z

@roberto I think this part is the problematic

mihaelkonjevic 2016-04-25T14:02:27.000068Z

it seems that you’re calling get-collection before load-recordings is finished

roberto 2016-04-25T14:02:51.000069Z

get-collection is supposed to be the default case

roberto 2016-04-25T14:03:08.000070Z

so, when an item is selected load-recordings shouldn’t be called, because it is already there

mihaelkonjevic 2016-04-25T14:04:09.000071Z

but you load only one item when the item is selected, right?

mihaelkonjevic 2016-04-25T14:05:30.000072Z

so basically what needs to happen is: both controllers need to load their data, list and current

mihaelkonjevic 2016-04-25T14:05:37.000073Z

and they will be merged inside the EDB

mihaelkonjevic 2016-04-25T14:05:53.000074Z

so you will always have only one version of the current recording

mihaelkonjevic 2016-04-25T14:06:11.000075Z

but, you still need to load the collection :list and named item :current

mihaelkonjevic 2016-04-25T14:07:05.000076Z

which is different from the place-my-order app

mihaelkonjevic 2016-04-25T14:07:14.000077Z

because it doesn’t have the master-detail view

mihaelkonjevic 2016-04-25T14:07:25.000078Z

it’s either the list of restaurants or one restaurant

mihaelkonjevic 2016-04-25T14:10:21.000079Z

do you see both requests (list and current) in the console when you have the detail page open?

roberto 2016-04-25T14:16:43.000080Z

what do you mean by ‘requests’?

roberto 2016-04-25T14:16:46.000081Z

xhr rquests?

mihaelkonjevic 2016-04-25T14:16:50.000082Z

yeah

roberto 2016-04-25T14:17:00.000083Z

no, it is not supposed to issue an xhr request

roberto 2016-04-25T14:17:07.000084Z

because it is already loaded.

roberto 2016-04-25T14:17:19.000085Z

so the flow is the following: initial view displays the master list, that does an xhr request

roberto 2016-04-25T14:17:43.000086Z

then when we select an item from that list, we don’t do any xhr requests because the items are already downloaded

mihaelkonjevic 2016-04-25T14:18:29.000087Z

oh, there’s another problem I just noticed

mihaelkonjevic 2016-04-25T14:18:37.000088Z

you’re returning the whole route-data

mihaelkonjevic 2016-04-25T14:18:43.000089Z

from the params function

mihaelkonjevic 2016-04-25T14:18:54.000090Z

which causes the controller to be restarted

mihaelkonjevic 2016-04-25T14:19:13.000091Z

controller manager checks what is returned from the params function

roberto 2016-04-25T14:19:14.000092Z

hmmm, so what should params return?

roberto 2016-04-25T14:19:24.000093Z

Receives the route-params and returns either the params for the controller or nil

mihaelkonjevic 2016-04-25T14:19:25.000094Z

only the subset of data your controller cares about

roberto 2016-04-25T14:19:31.000095Z

oh

mihaelkonjevic 2016-04-25T14:19:54.000096Z

this seems to be a documentation issue.

mihaelkonjevic 2016-04-25T14:20:50.000097Z

basically, controller checks what is returned from the params function

mihaelkonjevic 2016-04-25T14:21:01.000098Z

and based on that decides what to do with the controller

mihaelkonjevic 2016-04-25T14:21:15.000099Z

Whenever the URL changes, the manager controller will do the following: It will call params function of all registered controllers It will compare returned value to the last returned value (returned on the previous URL change) Based on the returned value it will do the following: If previous value was nil and current value is nil it will do nothing If previous value was nil and current value is not nil it will start the controller If previous value was not nil and current value is nil it will stop the controller If previous value was not nil and current value is not nil but those values are same it will do nothing If previous value was not nil and current value is not nil but those values are different it will restart the controller

roberto 2016-04-25T14:23:33.000100Z

ok, this is helpful

roberto 2016-04-25T14:23:36.000101Z

thank you