-
Notifications
You must be signed in to change notification settings - Fork 362
Om Next FAQ
Provide a function to the :send
key of the reconciler's parameter map (along with :state
and :parser
). This will be a function which takes two parameters: the EDN of the query expression fragment that will be passed to the server, and a callback to handle the response. Om will provide the callback, your function just needs to make the request and ensure that the callback receives, as its one argument, data in the format of an EDN result of a query expression -- for example by simply reading a transit-json response from the server back into EDN. Example:
(defn transit-post [url]
(fn [{:keys [remote]} cb]
(.send XhrIo url
(fn [e]
(this-as this
(cb (t/read (t/reader :json) (.getResponseText this)))))
"POST" (t/write (t/writer :json) remote)
#js {"Content-Type" "application/transit+json"})))
The transact!
function takes an initiating component and a query expression that consists of mutations and any number of reads. The first question to ask yourself is: does the transaction affect keys that are not part of the initiating component's query? Om implicitly updates the initiating component from the local app state after the transaction but if any keys outside the component are affected, they need to be added to the transact! query expression explicitly. An example: component
Foohas a query
[:foo/bar :foo/baz]and performs the transaction
(om/transact this [(app/do-something)])
. If mutate 'app/do-something
changes :foo/bar
or :foo/baz
, the component will be rerendered with the new data after the transaction. If it changes:something/else
however, the code has to be changed to (om/transact this
[(app/do-something) :something/else])`.