从这两个负责获取当前用户信息的处理程序开始:
(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))
我应该如何构造这段代码?