从这两个处理程序开始,它们负责获取当前用户信息:
(re-frame/register-handler
:got-user
(fn [db [_ user]]
(assoc db :user user)))
(re-frame/register-handler
:get-user
(fn [db [_]]
(ajax/GET "/user"
{:handler #(re-frame/dispatch [:got-user %1])})
db))
在一个页面中,我想显示朋友列表,但问题在于它取决于先获取用户:
(re-frame/register-handler
:get-friends
(fn [db [_]]
(when (nil? (:user db))
(re-frame/dispatch [:get-user]))
; Here's the problem, as I need to way for get-users and got-users to run.
(ajax/GET (str "/users/" (get-in db [:user :id]))
{:handler #(re-frame/dispatch [:got-friends %1])})
db))
我应该如何结构化这段代码?
[:get-user {:next :get-user-friends}]
,其中:next
是当我们得到用户时要分派的事件?:got-user
的处理程序将分派下一个事件。 因此,:get-friends
被分成两个部分::get-friends
(如果需要获取用户)和:get-user-friends
(获取已知用户的朋友)。 - coredump