在重新框架中,我有一个观点,实质上是:
(defn tool-panel []
(let [current-tool (re-frame/subscribe [:current-tool])]
(fn []
[:h1 (@current-tool "name")])))它会立即显示,但当前工具可能需要一段时间才能显示出来,除非以前加载了它,否则需要从服务器请求工具数据。在此期间,@currrent-tool为0,此代码崩溃。有什么合适的方法来处理这件事?我想这么做:
(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已经提供了一个模式,所以我想知道我是不是遗漏了什么。有没有一种不同的方式来构建一个应用程序,使用重新框架,最终不用抽象出我刚刚发现的这个模式?
我想,用更通用的术语,我可以说,你如何处理试剂应用程序中丢失的数据?
发布于 2015-08-11 13:51:34
我不认为你错过了太多。
组件的目标是呈现状态,如果该状态还没有存在,那么就没有什么可呈现的了,可能只有一条消息说“仍在加载.”。
在这里有一个Wiki页面:https://github.com/Day8/re-frame/wiki/Bootstrap-An-Application
https://stackoverflow.com/questions/31940875
复制相似问题