1

在重新框架中,我的观点基本上是这样的:

(defn tool-panel []
  (let [current-tool (re-frame/subscribe [:current-tool])]
    (fn []
      [:h1 (@current-tool "name")])))

它会立即显示,但 current-tool 可能需要一段时间才能显示,因为除非之前已加载,否则需要从服务器请求工具数据。在此期间,@currrent-tool 为 nil,此代码崩溃。处理这个问题的适当方法是什么?我想过这样做:

(defn tool-panel []
  (let [current-tool (re-frame/subscribe [:current-tool])]
    (fn []
      (when @current-tool
        [:h1 (@current-tool "name")]))))

哪个有效,但是在加载时会显示一个空白页面。如果我做:

(defn tool-panel []
  (let [current-tool (re-frame/subscribe [:current-tool])]
    (fn []
      (if @current-tool
        [:h1 (@current-tool "name")]
        [:h1 "Loading"]))))

我觉得这个视图正在扮演一个它应该有的额外角色:知道如何显示加载消息。此外,我可以看到这迅速演变成:

(defn tool-panel []
  (let [current-tool (re-frame/subscribe [:current-tool])
        foo (re-frame/subscribe [:foo]
        bar (re-frame/subscribe [:bar])]
    (fn []
      (if (and @current-tool @foo @bar)
        [:h1 (@current-tool "name")]
        [:h1 "Loading"]))))

这感觉像是一种常见的模式,我会一遍又一遍地重复。由于 re-frame 已经提供了一种模式,我想知道我是否遗漏了一些东西。有没有不同的方法来构建一个使用 re-frame 的应用程序,最终不必抽象出我刚刚发现的这种模式?

我想用更通用的术语来说,您如何处理试剂应用程序中丢失的数据?

4

1 回答 1

4

I don't think you are missing too much.

The goal of a Component is to render state, and if that state isn't there yet, then there's nothing to render, except perhaps a message saying "Still loading ...".

There is a Wiki page around this: https://github.com/Day8/re-frame/wiki/Bootstrap-An-Application

于 2015-08-11T13:51:34.817 回答