【问题标题】:Problems getting recursive component working in Om?递归组件在 Om 中工作时遇到问题?
【发布时间】:2014-03-28 23:10:08
【问题描述】:

我有以下几点:

(ns commentz.client
    (:require
     [om.core :as om :include-macros true]
     [om.dom :as dom :include-macros true]
     [clojure.browser.repl]))

(def app-state
  (atom
   {:id "a"
    :value "I am the greatest of comments!"
    :user "1"
    :anonymous false
    :score 10
    :children
    [{:value "I am ok too."
      :user "1"
      :anonymous false
      :score 4
      :children
      [{:value "I am the second greatest comment"
        :user "2"
        :anonymous false
        :score 7
        :children {}}]}
     {:value "I like turtles"
          :user "3"
          :anonymous true
          :score -3
          :children {}}]}))

(defn header [app owner]
  (dom/div #js {:className "header"}
           (dom/div #js {:className "vote"}
                    (dom/div #js {:className "up"})
                    (dom/div #js {:className "down"}))
           (dom/a #js {:className "username" :href (:user app)} (:user-name app))
           (dom/div #js {:className "score"} (:score app))))

(defn footer [app owner]
  (dom/div #js {:className "footer"}
           (dom/a #js {:className "permalink" :href (str "#" (:id app))} "permalink")
           (dom/a #js {:className "reply"} "reply")))

(defn comment [app owner]
  (reify
    om/IRender
    (render [this]
      (dom/div {:id (:id app) :className "comment"}
               (header app owner)
               (dom/div #js {:className "value"} (:value app))
               (footer app owner)
               (om/build-all comment (:children app))))))

(om/root comment app-state {:target (. js/document (getElementById "app"))})

上面的代码确实编译成功,但我没有看到任何递归。相反,当我使用浏览器进行检查时,我看到了以下内容。

10 
I am the greatest of comments! 
permalink reply 
0 32374988

我认为 32374988 可能是一个对象哈希,不确定 0 是什么意思。无论如何,我的意图是看到所有 4 个 cmets 都出现,其中一些评论嵌套在其他评论中。目前我只得到根评论,加上一些奇怪的0 32374988,递归构建的 cmets 应该在哪里。任何帮助表示赞赏。谢谢。

【问题讨论】:

  • 我犯了同样的错误,我是通过谷歌搜索“32374988”到这里的,哈哈

标签: recursion clojure clojurescript om


【解决方案1】:

(om/build-all) 返回一个序列。试试(apply dom/div nil ...)

(apply dom/div {:id (:id app) :className "comment"}
           (header app owner)
           (dom/div #js {:className "value"} (:value app))
           (footer app owner)
           (om/build-all comment (:children app))))))

【讨论】:

    猜你喜欢
    • 2011-12-23
    • 2022-01-15
    • 2020-05-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 2020-04-29
    相关资源
    最近更新 更多