【问题标题】:ClojureScript : How to access an object in an Om cursorClojureScript:如何访问 Om 游标中的对象
【发布时间】:2015-10-02 04:20:54
【问题描述】:

我只是在试验 Om,从基础教程中学习。

我的程序数据位于单个原子中:app-state。

如果我将一个序列放入该原子,例如。

(defonce app-state (atom {:things [1 2 3 4]}))

然后,在我的渲染中,我可以得到一个光标

(let [things (:things data)] ... )

并在其上绘制地图:

(map #(* % %) things)

但是,如果我有一个可以返回序列的方法的 defrecord:

(defrecord ThingCollection [xs]
    IThingCollection
    (getSeq [this] ... ) )

并将其放入应用程序状态

(defonce app-state (atom {:things (->ThingCollection [1 2 3 4]})))

...

(let [things (:things data)] ... )

我做不到:

(map #(* % %) (.getSeq things))

它只是抛出一个错误,说游标没有方法 .getSeq

这是有道理的。但是,我怎样才能真正获得光标内的 ThingCollection 对象呢?我找不到任何这样的例子。而且我尝试过的任何东西似乎都没有给我。

【问题讨论】:

    标签: clojurescript om figwheel


    【解决方案1】:

    试试(getSeq things)

    几点说明:

    1. 更喜欢 kebab-case get-seq 而不是 getSeq
    2. ->ThingCollection 没有给出对象,它只是给出了一条记录。这仍然是不可变的,大致只是一个“命名映射”。
    3. 一旦您意识到第 2 点,使用 .method 调用看起来应该是错误的,因为那是用于处理 JavaScript 对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多