In Om's basic tutorial, there is an example of using multimethod to dispatch
different components:
------- Begin Code Snippet -------
(defn student-view [student owner]
(reify
om/IRender
(render [_]
(dom/li nil (display-name student)))))
(defn professor-view [professor owner]
(reify
om/IRender
(render [_]
(dom/li nil
(dom/div nil (display-name professor))
(dom/label nil "Classes")
(apply dom/ul nil
(map #(dom/li nil %) (:classes professor)))))))
(defmulti entry-view (fn [person _] (:type person)))
(defmethod entry-view :student
[person owner] (student-view person owner))
(defmethod entry-view :professor
[person owner] (professor-view person owner))
------- End -------
The example above is fine, but I find it problematic if I wanted to have local
state in either student-view or professor-view.
First, local state in student-view is also visible in professor-view since they
are rendered as the same component: entry-view.
Second, if I wanted to implement om/IInitState, I will have to implement it in
the component that will be rendered first. For example, if I modified
student-view to be;
------- Begin Code Snippet-------
(defn student-view [student owner]
(reify
om/IInitState
(init-state [this]
{:student-id 1})
om/IRenderState
(render-state [_ state]
(dom/li nil (:student-id state)))))
------- End -------
And in the UI, if professor-view is dispatched first, (init-state) in
student-view won't get called. So I'd have to put student-view's initial local
state in professor-view.
For these reason, I wonder if multimethod should be avoided for components with
local state, or if there are ways to keep local states independent in
multimethods?
Cheers
Feng
--
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.