How to correctly use om/transact! to add a new entry to a list

918 views
Skip to first unread message

Roger Gilliar

unread,
May 7, 2014, 1:16:35 AM5/7/14
to clojur...@googlegroups.com
services is a cursor to an array.

The following works:

(let [new-services (conj @services {:name (str "New " (inc (count @services)))})]
(om/update! services new-services))

the following doesn't

(om/transact! services conj {:name (str "New Service ")})

and gives me:

Uncaught Error: Vector's key for assoc must be a number.

Why ?

Daniel Kersten

unread,
May 7, 2014, 2:30:26 AM5/7/14
to clojur...@googlegroups.com
om/transact takes a path as its third argument (a key or a vector of keys), but you are passing in a function (conj):


With om/update! you are using the 2 parameter variant, which is why that works: https://github.com/swannodette/om/wiki/Documentation#update

The versions of om/transact! are:

(om/transact! cursor function)
(om/transact! cursor path function)
(om/transact! cursor path function tag)

Note that it's not like swap! where you can pass additional arguments to function after the function.



--
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 clojurescrip...@googlegroups.com.
To post to this group, send email to clojur...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojurescript.

Toni Helenius

unread,
May 7, 2014, 5:06:37 AM5/7/14
to clojur...@googlegroups.com

(om/transact! services (fn [services] (conj services {:name (str "New service")})))

transact! is a bit different from swap because it is more focused on updating just a part of the map that is the app state.

Andrew Stoeckley

unread,
Jul 8, 2014, 3:04:43 AM7/8/14
to clojur...@googlegroups.com
On Wednesday, May 7, 2014 2:30:26 PM UTC+8, Daniel Kersten wrote:

> The versions of om/transact! are:
>
> (om/transact! cursor function)
> (om/transact! cursor path function)
> (om/transact! cursor path function tag)
>
> Note that it's not like swap! where you can pass additional arguments to function after the function.
>

I'm trying to understand the line in the documentation that says:

om.core/transact! can be given additional args to pass to f.

This implies there is a multiple arity variant of transact! ? What is meant by this line in the docs, as I've never seen any examples of it.

Andrew

Dave Della Costa

unread,
Jul 8, 2014, 4:11:38 AM7/8/14
to clojur...@googlegroups.com
Hi Andrew,

I assume what was intended was the same as how update-in behaves:

http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/update-in

However, reading the source for Om makes it clear that this is not
supported:

https://github.com/swannodette/om/blob/master/src/om/core.cljs#L776-L790

So this must be a mistake in the documentation--perhaps transact! used
to do this but no longer does. I suspect whenever David added the tag
argument he removed this functionality.

In any case I've removed this line from the wiki docs.

DD

Andrew Stoeckley

unread,
Jul 8, 2014, 4:41:19 AM7/8/14
to clojurescript
Awesome, thanks for clearing this up.
> --
> Note that posts from new members are moderated - please be patient with your first post.
> ---
> You received this message because you are subscribed to a topic in the Google Groups "ClojureScript" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojurescript/ziMtDNJP2lg/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to clojurescrip...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages