i'm a clojure noob and am taking a look at the om next tutorial
trying some random refactors and writing things a different way, etc
i can't get the above to work because of the (om/transact! this '[(sym)])
line
i don't understand exactly what format of data i should be trying to send through
fwiw the source line is (om/transact! this '[(increment)])
i understand why the way i have sym
in its present form isn't correct, i just can't figure out how to change it
@machty - did you solve your problem?
I wouldn't name the mutation after a function, but rather a key in a state. For your case, incrementing and decrementing, just use a mutation like 'update-click-value! and give it a parameter with the value. :onClick (fn [] (om/transact! this [(list 'update-click-value! {:count (inc count)})]) and in the mutate method swap! the global state with the value from the parameter :count. By using parameters you save yourself from writing 2 transactors for increment and decrement, and you could do anything with the value in the global state as you please.
@hlolli that sounds good to me, but for now i'm just exploring all the different ways i can express things and, in short, i can't figure out what format of data/payload transact!
is expecting. The example code provides a quoted vector of a list of a symbol (om/transact! this '[(increment)])
so given that i have an already-quoted symbol stashed in sym
, how do i pass that through in a way that'll work?
@machty - examples:
(om/transact! this `[(foo/bar ~{:value true})])
(defmulti mutate om/dispatch)
(defmethod mutate 'foo/bar
[{:keys [state] :as env} key {:keys [value]}]
{:action #(swap! state assoc :foo-bar value)})
or something
@am-a_metail can you provide an example using only sym
, where sym is already bound to a Symbol?
(i'm sorry if i'm being difficult, some of this probably has to do w the fact that i don't understand how to talk about clojure just yet... not sure if sym
is a variable/binding/something else)
So you want to define in terms of sym
or what sym
is bound to?
refer to my original example
the symbol is never resolved, its just functions as key, visually better than actually a key, given that you are using defmulti.
i use a let statement to destructure a quoted symbol ('increment, 'decrement, 'double) into the sym
binding
and now i want to pass along whatever's in sym
to transact
ah ok, in that case this is just unquoring. But these symbols in your example dont seem to be bounded to anything. If 'increment is a dispatch on multimethod, than there's nothing to deref.
just a fun fact still
(def a 666)
(let [b `a]
(eval b))
=> 666
or
(defn c [] (+ 2 2))
(let [e `c]
~(c))
ahh dont have repl open and cant confirm this....i don't see where i'm using a multimethod
maybe i should take a step back
it's my understand that you pass a "mutation query" to transact
and the mutate
fn provided to the reconciler teases apart that data and decides how to change app state
Yes, you should understand the parser, everything in transact! is sent to the parser, same for reads. And within the parser you have :read and :mutate, they expect two functions, so to break each function down we use multimethod, otherwise you could have long (case dispatch-key ....blabla...). But as style om.next users tend to use quoted symbols for mutate and keys for read (makes it look as its a function and keywords makes it looks as if you are reading from a hash-map).
i believe i follow so far
for now i am actually just trying to get the naive redux-y giant case statement approach working
and then i will try more things
ok ok, dont hesitate to ask on this channel tough.
ah just got it to work!
had to use list
i guess that makes sense
it gives me the desired granularity that the quoting syntax didn't
i was trying something like [(sym)]
and i believe it was trying to IFn call the sym
which i think does a hash lookup but it wasn't being passed anything so it was getting an arity error
the world is slowly making sense
ok, I think you weren't doing anything wrong, I see now what you ment, just confused me that you are using sym (for symbol name) for this. But backtick ` with ~ is another way like am-a-metall suggested. If you need to dereference then dont use '
> But as style om.next users tend to use quoted symbols for mutate and keys for read (makes it look as its a function and keywords makes it looks as if you are reading from a hash-map)
@hlolli i'm still trying to parse what this means
actually i think i just need to see a more involved example of mutation than what's on https://github.com/omcljs/om/wiki/Quick-Start-(om.next)
Well, you could do (om/transact! this `[(:update-bla)]) but doesn't look right. A key rarely acts as function, but can. Its up to you if you want to use quoted symbols or not.
ok, so the point of `[(do-stuff)] is to make it kinda look like do-stuff is a fn with side effects?
it does something
whereas :symbol looks closer to, say, reading from a hash
yes, the multimethod its a member of, not the dispatch symbol/key itself. You can dispatch on many different things.
in the end, they're both just keys that the reconciler fns have to parse, whether via a giant (case) or multimethods
do you pretty much always use (defmulti mutate om/dispatch)
?
yes, I never know how much the app will grow, and switching from projects without changing habits.
has anyone tried porting the om.next model to a javascript lib (similar to how redux is pretty much a js port of elm's)?