On Thursday, September 4, 2014 9:50:41 PM UTC-5, Tom George wrote:
> Hi,
>
> I'm doing my first real work in clojurescript/om. I have a little web
> service that queries a database and returns a JSON message of all known
> schema owners. I want to use this data source to populate a dropdown.
>
> The demo page for om-bootstrap is really nice, but in the examples for
> populating a dropdown, the menu-items are known beforehand, so they just nest
> a lot of (menu-item) calls. I won't know the size of my dropdown until I hit
> the database, and I don't want to spell out each individual menu item.... Is
> there a better way to do this? I was thinking maybe creating a map of
> number-string pairs, where the keys are menu item keys, and the values are
> the schema owners. However, I can't think of how I would get around
> specifying each individual menu item. I tried mapping the menu-item function
> to no avail.
>
> The relevant clojurescript looks like this right now:
>
> (ns junk-browser.core
> (:require-macros [cljs.core.async.macros :refer [go]])
> (:require [cljs.core.async :refer [>! <! chan close!]]
> [om.core :as om :include-macros true]
> [om.dom :as dom :include-macros true]
> [om-bootstrap.button :as b]
> [cljs-http.client :as http]))
>
>
> (defn schema-dropdown [app owner]
> (reify
> om/IInitState
> (init-state [_]
> {:owners []})
> om/IWillMount
> (will-mount [_]
> (go
> (let [owners (into [] (<! (schema-owners)))] ; (schema-owners) is
> a cljs-http GET
> (om/update-state! owner #(assoc % :owners owners)))))
> om/IRenderState
> (render-state [ _ state]
> (dom/div nil
> (b/dropdown {:title "Schema Owners"}
> (b/menu-item {:key 1} (nth (:owners state) 0
> :not-found
> ...
> (b/menu-item {:key n} (nth (:owners state) n-1
> :not-found)))))))
>
>
>
> Any ideas on how I can tackle this in a better way?
>
> Thanks,
>
> Tom
I was typing up my answer as Sean was posting his, but I guess it can't hurt to
post a few more examples...
Warning: I'm also new to Om & ClojureScript so I'm not at all confident that
these examples are idiomatic.
In each of these, you should be able to use your existing "schema-owners" ajax
function in place of mine.
Here's an example similar to what you pasted:
https://www.refheap.com/89834
You're using local state for what really seems to me more like app state.
Here's an example using app state instead:
https://www.refheap.com/89835
If your data doesn't have unique keys, you could try something like this:
https://www.refheap.com/89836
or this:
https://www.refheap.com/89837
That's as far as I've gotten with this stuff. Hope that helps.
--
Note that posts from new members are moderated - please be patient with your
first post.
---
You received this message because you are subscribed to the Google Groups
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/clojurescript.