【问题标题】:om ClojureScript ref-cursor error when trying to update vector using om/transact尝试使用 om/transact 更新向量时 om ClojureScript ref-cursor 错误
【发布时间】:2014-12-07 15:35:31
【问题描述】:

我在尝试更新游标时看到以下错误:

Uncaught Error: No protocol method ITransact.-transact! defined for type function: function comments(){return om.core.ref_cursor.call(null,new cljs.core.Keyword(null,"comments-data","comments-data",1871210833).cljs$core$IFn$_invoke$arity$1(om.core.root_cursor.call(null,cljs_playground.core.app_state)));

我在我的应用程序状态中使用 ref-cursor 指向 cmets-data 向量:

(def app-state
  (atom
    {:comments-data [{ :author "Commenter 1" :text "comment 1" }
                     { :author "Commenter 2" :text "comment 2" }]}))

(defn comments []
  (om/ref-cursor (:comments-data (om/root-cursor app-state))))

现在,我想在用户通过我的输入组件中的输入提交一些数据时更新此光标。我使用 (let [foo (->> cmets)] 来引用渲染状态中的光标。我将它传递给一个处理用户提交的函数调用,我想简单地向这个向量添加一个项目。

(defn handle-submit [e owner {:keys [text]} foo]
  (om/transact! foo #(concat % {:author "foo" :text "bar"}))
  (om/set-state! owner :text ""))

(defn Input
  [data owner]
  (reify
    om/IInitState
    (init-state [_]
      {:text nil})
    om/IRenderState
    (render-state [this state]
     (let [foo (->> comments)]
       (dom/div nil
         (dom/input #js
           { :type "text"
             :ref "text-field"
             :value (:text state)
             :onChange (fn [event] (handle-change event owner state))})
         (dom/button #js 
           { :onClick (fn [event] (handle-submit event owner state foo))} "submit"))))))

但是,当我进行 om/transact!打电话给我看到上面的错误。

【问题讨论】:

    标签: clojure reactjs clojurescript cursors om


    【解决方案1】:

    您想调用 comments 函数并使用其返回值(光标),而不是使用函数本身。更改此行:

     (let [foo (->> comments)]
    

    到:

     (let [foo (->> (comments))]
    

    这应该适合你。

    【讨论】:

    • 嗨@rmunn,首先谢谢你。其次,我做了你建议的改变。我刷新了页面,但现在我看到页面上没有显示我的任何内容。我没有看到编译器输出任何错误,但我确实看到了 Uncaught TypeError: Cannot read property 'call' of undefined 在我的控制台中。我创建了一个更新的要点,其中包含我正在做的这个演示的所有代码。我认为这是正确的轨道,只是不确定为什么它不能正确显示。 gist.github.com/mjw56/25e126f45a29ee58f313
    • @user16626 - 我建议将此作为 Stack Overflow 上的一个单独问题提出,因为这似乎是一个不同的问题。但我的猜测是接近末尾的(om/build Input ((get global :comments-data) this))) 行是错误的。首先,你有错误的括号 - 你得到了 :cmets-data 键,然后 调用 它使用 this 作为参数?几乎可以肯定不是你打算做的。其次,您的my-app 中的任何地方都没有定义this
    • @user16626 - 我即将离开互联网几天。如果您对此代码有更多问题,您应该将the gist you posted 转为另一个问题,以便其他人可以帮助您解决问题。因为如果你再给我写信,我要到 12 号星期五才能看到。
    【解决方案2】:

    改变

    (let [foo (->> comments)]
    

    (let [foo (comments)]
    

    并保持简单。不需要->> 运算符。

    【讨论】:

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